Rahul Chawla Rahul Chawla - 7 months ago 15
HTML Question

Adding argument to form action using input type hidden

How to add arguments in a form action using input type="hidden".
My HTML snippet...

<form id="form1">
<input type="text" name="txt"/>
<input type="hidden" name="usID" value=123/>
<input type="hidden" name="usname" value="name1"/>
<input type="submit" onclick="add()" value="ADD"/>

My JavaScript snippet...

function add()

document.getElementById('form1').action = "/page";

After entering "text" in the textbox and pressing ADD, the link looks like this...

Why hasn't the usID and usname changed to 789 and "name2" respectively?
Any other alternative if not this?


getElementsByName is going to return a collection of html elements (NodeList), not a single html element. meaning the return value doesnt have a value attribute that will change the input. you need to either give them an id and find them with getElementById and then change the value, or grab the first element of your collection


or (preferred way)

<input type="hidden" name="usname" value="name1" id="usname"/>

function add(){

though i have to ask, is there a reason you're changing the hidden field element like this?