I have a button on a form and when it's clicked I wish to find the selectedvalue from a select element using JQuery. Can anyone help?
I have a button on a form and when it's clicked I wish to find the selectedvalue from a select element using JQuery. Can anyone help?
let's assume this html code:
<form id ='formId'>
<select id='selectBox' name='selectBox'>
<option value='1'>option 1</option>
<option value='2'>option 2</option>
</select>
<input type='button' value='go' id='goButton' />
the jquery code it will be this:
$(document).ready( function(){
$("#formId").submit(function(){
return false; // disable the regular submit
});
$("#goButton").click( function(){
var desiredValue = $("#selectBox").val();
alert( desiredValue);
});
});
another way is to detect the change as it happens:
$(document).ready( function(){
$("#selectBox").change( function(){
var desiredValue = $(this).val();
alert( desiredValue);
});
});