I’m trying to make a click event on submit button on each option element of select.
Here is my script:
JavaScript
x
7
1
var selectObj = $("#selectObj");
2
3
selectObj.find("option").each(function(){
4
selectObj.val($(this).val());
5
$("#submitBtn").click();
6
});
7
But, it just work on the last option element.
Advertisement
Answer
But, it just work on the last option element.
I don’t know what you are trying to achieve, but your code should work.
Also not sure about the usage of selectObj.val($(this).val());
JavaScript
1
10
10
1
var selectObj = $("#selectObj");
2
3
selectObj.find("option").each(function(){
4
//selectObj.val($(this).val());
5
$("#submitBtn").click(myFunc($(this).val()));
6
});
7
8
function myFunc(val){
9
console.log(val);
10
}
JavaScript
1
7
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<select id="selectObj">
3
<option value="first">First</option>
4
<option value="second">Second</option>
5
</select>
6
7
<button type="button" id="submitBtn" >Test</button>