0

I coded this today, but when i use my dropdown i doesn't work... it works fine when I'm writing the numbers myself. Why?!

  function favBrowser() {
    var mylist=document.getElementById('myList');
    document.getElementById('favorite').value=mylist.value;
}
 function bar() {
document.write('Hello World!');


<select id="myList" onchange="favBrowser()">
    <option></option>
    <option value="a">a</option>
    <option value="b">b</option>  
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
</select>
<p>heheheh: <input type="text" id="favorite" size="20" oninput="return bar()" onValueChanged="return bar()" value=""></p>
1

4 Answers 4

1

I think this is what you're looking for

function favBrowser(list) {
    document.getElementById('favorite').value=list.value;
}


<select id="myList" onchange="favBrowser(this)">
    <option></option>
    <option value="a">a</option>
    <option value="b">b</option>  
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
</select>
<p>heheheh: <input type="text" id="favorite" size="20" value=""></p>
Sign up to request clarification or add additional context in comments.

2 Comments

when i use that my dropdown doesnt print de value from dropdown , only undefined ? ;o
You mean the input value doesn't print the value from the dropdown? Can you please show the onchange you used?
0

select elements don't have a value, option elements in it have a value. Try the following instead :

document.getElementById('favorite').value=mylist.options[myList.selectedIndex].value;

not tested, but that's the idea

Comments

0

Having this code html code:

<select id="myList" onchange="favBrowser(this.value)">
    <option></option>
    <option value="a">a</option>
    <option value="b">b</option>  
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
</select>

<input type="text" id="favorite" size="20" value=""/>

You can do the next:

function favBrowser(val) {
     document.getElementById('favorite').value=val;
}

Every time that you changes the value of drop down list, automaticly changes the value of input because, you are sending the value of drop down list as param.

4 Comments

my problem is , when my dropdown changes the value nothing happens , but when i write my own value in de input document.write('Hello World!'); displays
Dont use document.write after the page loaded.
document.write was only a thing i wrote to se the change when input has an value :)
but document.write overwrite all data that you had in a document
0

First, there is no event onValueChanged; you're looking for onchange.

The reason why bar() is not being triggered when your drop-down script changes the value of favorite is because oninput is triggered when the value of the element is changed through the user interface (not a script), and onchange is triggered when blur occurs (that is, the input element loses focus). So, in order to get the results you want, you'll need to call bar() from favBrowser():

function favBrowser() {
  var mylist=document.getElementById('myList');
  document.getElementById('favorite').value=mylist.value;
  bar();
}

2 Comments

hmmm , can u write an example? I'm new on this :(
The point is, you can't use oninput or onchange to do something after a script changes the value of an element like you're trying to do. Instead you need to just have the script do it.

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.