javascript obtain value of select option without submitting form -


i'm trying value of input select option without submitting form (need keep available while displaying live results). need use value global variable inserted in array later on. because can't use submit, need find way update value if user changes option (ie using onchange).

here's have (doesn't work):

html

<select id="color" >     <option value="">select:</option>     <option value="blue">blue</option>     <option value="red">red</option>     <option value="yellow">yellow</option> </select> 


javascript

document.getelementbyid('color').onchange = function() {     var colorinput = this.value;     console.log(colorinput); }; 


updated

okay, no using inline javascript, it. updated script above reflect of answers i'm getting - not addressing central problem. tracking input option value within script no problem; need value global variable. reason can't it.

here's jsfiddle analogous how need use it: http://jsfiddle.net/agvrn/2/

it should able track select option (already can) , must able use value in separate function - in case inserting paragraph field "show".

don't use inline javascript.

try (avoiding global variables):

(function() {     var select_val;     document.getelementbyid('color').addeventlistener('change', function() {         select_val = this.value;         document.getelementbyid("show").innerhtml = select_val;     }, false);  })(); 

demo


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -