Ed Lynch Ed Lynch - 2 months ago 7
HTML Question

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.

Answer

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>