I am trying to get the actual string that is placed in a form action. The problem is when I do this, the action property resolves to an absolute path even though a relative path is in the HTML. How do I get the actual string that is in the action property?
Here is a sample of what I am referring to: http://jsfiddle.net/MSY4s/
3 Answers 3
If you're already using jquery, I would use the .attr function rather than extracting the DOM element from the jQuery object. Like so:
$("form").attr("action");
That should give you literally what is in the action attribute. In the example you provided, that should look like "/somewhere". The second example in your jFiddle will show a full path since that's what is in the action attribute.
4 Comments
Relative URLs are always resolved to absolute ones on the base of the current document’s URL.
4 Comments
jQuery.attr or getAttribute.jQuerygetAttribute and that doesn't evaluate to an absolute URL. It might be the difference between attributes and properties, as lonesomeday pointed out.Try this:
Give both of your forms ids:
<form id="form1" action="/somewhere" method="post">
<input type="text" name="test" />
</form>
<form id="form2" action="https://fiddle.jshell.net/somewhere2" method="post">
<input type="text" name="test" />
</form>
Then using these ids you can get the action attribute of each form:
$('#form1').attr('action');
$('#form2').attr('action');
You can also set the action attributes using the same tags:
$('#form1').attr('action', '[New Action Value]');
$('#form2').attr('action', '[New Action Value]');
Hope this helps.