Forum Moderators: open

Message Too Old, No Replies

How to update 2 textboxes with javascript ?

         

rider_1

4:09 am on Jan 4, 2012 (gmt 0)

10+ Year Member



Mates,

How can I update a multiple textboxes value with javascript, when user change the value of textboxes, and I have a value already on those textboxes ?

<input type="text" id="amount_textbox" onChange="UpdateValue()" value="100">
<input type="text" id="amount_textbox" onChange="UpdateValue()" value="200">


function UpdateValue()
{
//alert ("you have changed the textbox...");
var x = document.getElementById('amount_textbox').value;
document.getElementById("amount_textbox").setAttribute("value", x);
document.getElementById('amount_textbox').value = x;

}

}


That function is working only for the first textbox, but how can I make other textbox worked ?

Thanks

daveVk

11:08 am on Jan 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="text" id="amount1_textbox" onChange="UpdateValue(this)" value="100">
<input type="text" id="amount2_textbox" onChange="UpdateValue(this)" value="200">

function UpdateValue(el)
{
//alert ("you have changed the textbox...");
var x = el.value;
el.value = x; // should this be the other textbox ?
}

You should not use the same id on multiple elements.

Passing (this) into function simplifies matters, the ids are no longer needed for this bit of code at least.

setting attribute value redundent ?

rider_1

12:44 am on Jan 5, 2012 (gmt 0)

10+ Year Member



it didn't work fine, I've tried at
[jsfiddle.net...]

daveVk

3:15 am on Jan 5, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try [jsfiddle.net...]