Entering text in a text box

Can someone tell me why this jsFiddle is not working?

The idea is simple - just enter the selected text in the text box.

HTML:

<input type="text" id="teste" maxlength="50"> <select> <option onClick="nelson">nelson</option> </select> 

JavaScript:

 function nelson(){ document.getElementById('teste').value =+ "nelson"; } 

Thanks in advance

+4
source share
6 answers

DEMO: jsFiddle

HTML:

 <input type="text" id="teste" maxlength="50" /> <select onchange="selectedItemChange(this)"> <option value="nelson">nelson</option> <option value="justin">justin</option> </select> 

JS:

 function selectedItemChange(sel) { document.getElementById('teste').value = sel.value; } 

Explanation:

 <option onClick="nelson">nelson</option> 
  • has been modified for three reasons:

    • onclick preferred over onclick for consistency
    • nelson needs to be changed to nelson() to actually call the function.
    • Since we are dealing with a select html element, it is better to use the onchange event in the root.

document.getElementById('teste').value =+ "nelson";

  • As many others pointed out, the correct operator is += or =

To set the initial value, do the following

DEMO: jsFiddle

HTML

 <input type="text" id="teste" maxlength="50" /> <select id="select-people" onchange="selectedItemChange(this)"> <option value="nelson">nelson</option> <option value="justin">justin</option> </select> 

Js

 function selectedItemChange(sel) { document.getElementById('teste').value = sel.value; } window.onload=function(){ document.getElementById('teste').value = document.getElementById("select-people").value; } 
+8
source

No operator =+ , += - this is what you need.

+5
source

First of all, the right operator += not =+ .

The reason this does not work is because your function must be called :)

Just let the closure run instantly:

 (function nelson(){ document.getElementById('teste').value += "nelson"; })(); 

or without function :

 document.getElementById('teste').value += "nelson"; 

or call it anywhere:

 nelson(); 
+2
source

You are mistaken, change =+ to += .

Try using a recommender event for select . This is change .

 <input type="text" id="teste" maxlength="50"> <select onchange="nelson(this.value);"> <option>nelson</option> </select> 

And js

 function nelson(value){ document.getElementById('teste').value += value; } 
+1
source

This is your complete working solution, it will work with your code.

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <input type="text" id="teste" value="" /> <select onchange="nelson()" id="sel_nel"> <option value="">select</option> <option value="nelson">nelson</option> </select> <script> function nelson(){ document.getElementById('teste').value = document.getElementById('sel_nel').value; } </script> </body> </html> 
+1
source

Look at this script: I assume that you want to change the text input value depending on the selection made in select -tag.

-> http://jsfiddle.net/mQyLG/2/

Refresh (Explanation):

  • I set the select value directly. If you really meant += , you can change it to input.value = select.value .
  • I used the onchange select event instead of the onclick event for each option . Like you, you save a lot of code;)
  • I call the method once at startup to provide an initial value there.
  • I changed the scope to fit the window . This is not a good idea and should fit your real area.
0
source

Source: https://habr.com/ru/post/1496108/


All Articles