Here is my javascript code, i want to pass current selected value of the option to my js function, in this code i used static number 6.
<select name='project_name' class='required input_field' id='project_name' onchange="sendRequest('GET','getClientName.jsp?ProjectId=6')">
<option value=''>-- Select --</option>
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>
help me to solve this...
Here is my javascript code, i want to pass current selected value of the option to my js function, in this code i used static number 6.
<select name='project_name' class='required input_field' id='project_name' onchange="sendRequest('GET','getClientName.jsp?ProjectId=6')">
<option value=''>-- Select --</option>
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>
help me to solve this...
Share Improve this question asked Sep 25, 2012 at 5:29 Raj AdroitRaj Adroit 3,8885 gold badges33 silver badges44 bronze badges 1- @Buzz i tried like this, onchange="sendRequest('GET','getClientName.jsp?ProjectId=this.value')" – Raj Adroit Commented Sep 25, 2012 at 5:31
3 Answers
Reset to default 7Change the string 'getClientName.jsp?ProjectId=6'
to
'getClientName.jsp?ProjectId=' + this.options[this.selectedIndex].value)
or
'getClientName.jsp?ProjectId=' + this.value)
but I think the first one is more browser patible.
var selectBox = document.getElementById('project_name');
selectBox.addEventListener('change', function() {
sendRequest('GET', 'getClientName.jsp?ProjectId=' + this.value);
});
It will help you to get the selected option value . Try this link http://jsfiddle/xyaaa/9/.
<html>
<head>
<script>
function getOption(t){
var val;
var options = t.options;
for(var i=0,len=options.length;i<len;i++){
var option = options[i];
if(option.selected){
val = option.value;
}
}
return val;
}
function sendRequest(method , url){
console.log(url);
}
</script>
</head>
<body>
<select onchange="var x=getOption(this);sendRequest('GET','getClientName.jsp?ProjectId='+x)">
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>
</body>
</html>