Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change value of input onchange?

I am trying to create a simple JavaScript function. When someone inserts a number in an input field, the value of another field should change to that value. Here is what I have at the moment:

function updateInput(ish) {       fieldname.value = ish;   }   <input type="text" name="fieldname" id="fieldname" />   <input type="text" name="thingy" onchange="updateInput(value)" />  

Somehow this does not work, can someone help me out?

like image 357
Jay Wit Avatar asked Mar 28 '11 10:03

Jay Wit


People also ask

How do I pass value to onChange?

To pass multiple parameters to onChange in React:Pass an arrow function to the onChange prop. The arrow function will get called with the event object. Call your handleChange function and pass it the event and the rest of the parameters.

How do I write onChange for select tag?

To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.

What is onChange attribute?

The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.


2 Answers

You can't access your fieldname as a global variable. Use document.getElementById:

function updateInput(ish){     document.getElementById("fieldname").value = ish; } 

and

onchange="updateInput(this.value)" 
like image 74
Alexey Romanov Avatar answered Oct 04 '22 05:10

Alexey Romanov


for jQuery we can use below:

by input name:

$('input[name="textboxname"]').val('some value'); 

by input class:

$('input[type=text].textboxclass').val('some value'); 

by input id:

$('#textboxid').val('some value'); 
like image 26
Aditya P Bhatt Avatar answered Oct 04 '22 06:10

Aditya P Bhatt