Ed Lynch
Ed Lynch

Reputation: 623

Reference the action field in JavaScript

How can I refference and then assign a new value to the text in the action field via JavaScript?

<form name="submitForm" action="THIS_TEXT">
    <input type="submit" class="button">See Results</input>
</form>

Thanks.

Upvotes: 1

Views: 301

Answers (3)

Manikant Gautam
Manikant Gautam

Reputation: 3591

You could try in such a way also.

<form name="submitForm" action="THIS_TEXT"> <input type="submit" class="button">See Results</input> </form>

And now javascript

 $('form[name="submitForm"]').attr('action','revised_action_url');

Upvotes: 0

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You could reference your form by name document.forms["submitForm"] then use setAttribute() function to set new text to the attribute you want :

document.forms["submitForm"].setAttribute("action", "new_text_here");

NOTE : Then input is a self-closing tag, so your input button should be like :

<input type="submit" class="button" value="See Result"/>

Hope this helps.

document.forms["submitForm"].setAttribute("action", "new_text_here"); //set action

console.log(document.forms["submitForm"].getAttribute("action")); //get action
<form name="submitForm" action="THIS_TEXT">
    <input type="submit" class="button" value="See Result"/>
</form>

Upvotes: 1

Robert Wade
Robert Wade

Reputation: 5003

This assuming you only have one form with the name submitForm. If you have multiple forms on the page, you should give them an id and use document.getElementById instead.

$forms = document.getElementsByName("submitForm");
$form = $forms[0];

$form.setAttribute("action", "NEW ACTION");
<form name="submitForm" action="THIS_TEXT">
    <input type="submit" class="button">See Results</input>
</form>

Upvotes: 0

Related Questions