Reputation: 4619
i am rendering a select box using Ajax call in php and the output is coming like
<select onchange = 'addtolist(this.val)' name='select_val' id='select_val' class= 'required'>
<option value='xzvfxcv'>xzvfxcv</option>
<option value='dfscs'>dfscs</option>
</select>
ok and here is my function addtolist
function addtolist(id){
alert(id);
}
now while changing the select box i am getting the undefined
please help me out out what might i am doing wrong ?
Upvotes: 0
Views: 224
Reputation: 148110
val
is not valid attribute of javascript element you have to use value
instead,
<select onchange = 'addtolist(this.value);' name='select_val' id='select_val' class= 'required'>
<option value='xzvfxcv'>xzvfxcv</option>
<option value='dfscs'>dfscs</option>
</select>
Upvotes: 2
Reputation: 8818
Try this
<select onchange = 'addtolist(this);' name='select_val' id='select_val' class= 'required'>
<option value='xzvfxcv'>xzvfxcv</option>
<option value='dfscs'>dfscs</option>
</select>
AND
function addtolist(id) {
alert(id.value);
}
Upvotes: 1
Reputation: 191729
You can use onchange
if you want, but I think this has been largely superseded by keeping scripting together. Get rid of onchange
altogether and just update your JavaScript:
$("#select_val").on('change', function () {
//or alert() if you must
console.log($(this).val());
});
Upvotes: 1