Change form's action attribute
It does work, you aren't testing it properly. Viewing source will show you what the source looked like when the page was loaded, not after it as modified by jQuery. If you alert()
action value after it is fetched with jQuery, you will see the new value:
alert($("#register").attr("action"));
It's also generally a good idea to make sure that the DOM has loaded before you make any changes, for which you can use ready()
:
$(document).ready(function(){ /* your code */ });
Check out: http://jsfiddle.net/wvYjs/2/
daGUY
Front-end web developer with 10 years of professional experience. Highly knowledgable of various web technologies, frameworks, and tools (HTML5, CSS/SCSS, JavaScript/jQuery, JSON, Zurb Foundation, Git, etc.). Multiple years of experience working in an Agile environment with Atlassian utilities (JIRA, Confluence, Stash, Bitbucket, etc.). Work experience ranges from tiny startups (employee #7!) to large international businesses (Mercedes-Benz, Nestlé), where I've written front-end code for a wide variety of projects including music streaming apps, corporate websites, and mobile-responsive landing pages and email campaigns.
Updated on November 01, 2020Comments
-
daGUY over 3 years
Okay, I must be missing something here because this should be extremely simple, yet it doesn't work...
I have a
form
on my page with an emptyaction
attribute (action="#"
). When the page is ready, I want to change theaction
attribute to something else. My code is very simple:HTML:
<form action="#" method="post" id="register"></form>
jQuery:
$("#register").attr("action", "http://www.test.com");
Even in a test fiddle, it doesn't work! View the source of the Result frame and you'll see the value of the
action
attribute is still#
.Where am I going wrong??
-
daGUY over 11 yearsGah, silly mistake on my part. I have a Safari plugin called BetterSource that can show you the generated HTML, but I was doing the regular "view source" instead. You're right, of course. Thanks! I need coffee...
-
doublesharp over 11 yearsIf you use the Developer Tools Inspect Element in Chrome or Firebug in Firefox it will do the same.