Rick Ant
Rick Ant

Reputation: 236

How to update 2 textboxes with javascript?

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?

Upvotes: 0

Views: 1094

Answers (5)

Rick Ant
Rick Ant

Reputation: 236

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;
}

Upvotes: 0

Adam Fowler
Adam Fowler

Reputation: 1751

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!

Upvotes: 0

AmbroseChapel
AmbroseChapel

Reputation: 12097

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?

Upvotes: 0

Manigandan Arjunan
Manigandan Arjunan

Reputation: 2265

in jquery

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

in javascript

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

document.getElementById('text').value = 'Blahblah';

Upvotes: 1

Slavic
Slavic

Reputation: 1962

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;
}

Upvotes: 0

Related Questions