Theun Arbeider
Theun Arbeider

Reputation: 5409

How do to I send my select box value to javascript

I have a random amount of select boxes generated by javascript, all containing the same options. Now I wish to have a "master-selectbox" which sets the value for each and every one of them.

Currently I have <select id="changeKlas" onChange="javascript:changeClass(this.parentNode, getElementById(changeKlas))">

At the javascript I've gotten as far as to find each and every select box and I already know how to set the value but the value is not being send to javascript.

On each attempt I have made the getElementById(changeKlas) is null. How can I fix this so I can get the text and value of the selected textbox in the given select?

Upvotes: 0

Views: 1527

Answers (3)

Manjeet
Manjeet

Reputation: 1

<select id="changeKlas" onChange="changeClass();">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="black">Black</option>
</select>

<script>
function changeClass()
{
 var changeKlas = document.getElementById('changeKlas').value;
alert(changeKlas);
}
</script>

// On change It will alert the SELECTED VALUE

Upvotes: 0

codeandcloud
codeandcloud

Reputation: 55200

Try this.

<select id="changeKlas" onChange="changeClass(this)">

And in your JavaScript, this will be transformed to document.getElementById(changeKlas)

Upvotes: 1

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

You need to quote the id: [...], document.getElementById('changeKlas') -- notice the single quote, double quote needs to be escaped because of the outer one from onChange. Also, notice that getElementById belongs to document

Upvotes: 0

Related Questions