0

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?

Am I doing in the right track on the code below?

<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, the second one can not update, how can I make other textbox work?

3
  • Why would you even let the user change the value of the textbox if you are going to revert the change? Wouldnt it be better to just disable the textbox? Commented Jan 3, 2012 at 9:13
  • in case they want to change the value inside the textbox, and i need that updated value being passed into the next form... Commented Jan 3, 2012 at 9:15
  • y do u need the last line document.getElementById('amount_textbox').value = x; when you have already set the value using document.getElementById("amount_textbox").setAttribute("value", x); Commented Jan 4, 2012 at 5:31

5 Answers 5

1

in jquery

$("#text").val("my new value");

in javascript

document.getElementById("text").setAttribute("value", "my new value");

document.getElementById('text').value = 'Blahblah';
Sign up to request clarification or add additional context in comments.

4 Comments

how can i put it my textbox ?
how can I get the new value from textbox?
function UpdateValue() { alert ("you have changed the textbox..."); //what I want : document.getElementById('amount_textbox').value = current value inside textbox; }
i want "my new value" equal to the value of current editbox value, how can i do that ?
0

First of all, I'm not sure why you'd want to set the value of the textarea(?) to itself - doesn't quite make sense, but here's the code nevertheless.

function checkTotal()
{
  var tbox = document.getElementById('ammount_textbox');
  var val = tbox.innerHTML;
  tbox.innerHTML = val;
}

3 Comments

how do you know it's not working? What behaviour do you expect?
i have tested it....i have editbox and there s a value there....and if user update the content of the checkbox, I want javascript updating the value of that checkbox....
checkbox? Since when are we speaking about checkboxes?
0

Your event handler is onClick, but it seems like you want to prevent changes? A click is not a change to the content of a form text box. Perhaps you want onChange?

2 Comments

I create like this : $('.amount_text_change').change(function() { alert('Handler for .change() called.'); });
how can I update the textbox value by the time user change the value inside textbox ?
0

change your onClick to onKeyup. With textareas, I have been able to access the value using just the value. Since the content is between opening and closing tags, you might be able to use the javascript innerHTML property ("That works with the button tag instead of value"). Good Luck!

2 Comments

can you give me an example, please ?
document.getElementById("myTextArea").innerHTML="This Text in Textarea";
0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.