I have a select tag with 4 options. I want to get a particular option clicked when the page loads and a method should called because of click event. Here what I have tried so far:
<select>
<option >Select an option...</option>
<option >Option 1</option>
<option >Option 2</option>
<option >Option 3</option>
<option id="lop" onclick="myFunction()">Option 4</option>
</select>
$(document).ready(function(){
$("#lop").trigger("click");
function myFunction(){
alert("hi");
}
});
You could try that:
$(document).ready(function(){
var opt = $("#a").val();
if (opt == 4) {
myFunction();
}
function myFunction(){
alert("hi");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value=''>Select an option...</option>
<option value=1>Option 1</option>
<option value=2 >Option 2</option>
<option value=3>Option 3</option>
<option id="a" value=4>Option 4</option>
</select>
Please change your Js with the following code:
$(document).ready(function(){
function myFunction(){
alert("hi");
}
$("#myselect").change(function() {
var opt = $(this).val();
if(opt == '4'){
myFunction();
}
});
var opt1 = $("#myselect").val("4");
if(opt1){
myFunction();
}
});
And remove onclick function from your option
<select id="myselect">
<option value=''>Select an option...</option>
<option value=1>Option 1</option>
<option value=2 >Option 2</option>
<option value=3>Option 3</option>
<option id="lop" value=4>Option 4</option>
</select>
Simply You can set the value of Select option dynamically like this
$(document).ready(function(){
$("#selectInp").val(4);
});
With HTML Provide an Id to your Select
<select id="selectInp">
<option value=''>Select an option...</option>
<option value=1>Option 1</option>
<option value=2 >Option 2</option>
<option value=3>Option 3</option>
<option id="lop" onclick="myFunction()" value=4>Option 4</option>
Updated fiddle: https://jsfiddle.net/j9o6ra3x/514/
OR If you want to work as your Solution Simply,
$("#lop").trigger("click"); // Instead of
$("#lop").prop('select',true); // Put this on ready
enter code here
You need to use on change
like below
$(document).ready(function(){
alert(this.value);
$('.selectList').on('change', function(){
alert(this.value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="selectList">
<option value=''>Select an option...</option>
<option value=1>Option 1</option>
<option value=2 >Option 2</option>
<option value=3>Option 3</option>
<option id="lop" onclick="myFunction()" value=4>Option 4</option>
</select>
This might help you, you should trigger a change event on select to trigger after the page load,
$(document).ready(function(){
$("#lop").trigger("click");
$('select').on('change', function(){
var val= $(this).val();
if(val == 4) myFunction();
})
});
function myFunction(){
alert("hi");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select>
<option value=''>Select an option...</option>
<option value=1>Option 1</option>
<option value=2 >Option 2</option>
<option value=3>Option 3</option>
<option id="lop" onclick="myFunction()" value=4>Option 4</option>
</select>
Shortest way do this:
$(window).load(function(){
$('select').val('3').trigger('change');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value=''>Select an option...</option>
<option value=1>Option 1</option>
<option value=2 >Option 2</option>
<option value=3>Option 3</option>
<option id="lop" onclick="myFunction()" value=4>Option 4</option>
</select>
You can use change
event to achieve the functionality you desire.
using .change()
$('#lop').change(function(){
//your function
})
using .bind()
$('#lop').bind('change',function(){
//your function
});
Then you can trigger change on page load:
$(function () {
$("select#myselect").change();
});
I created my own method which basically capitalizes the first alphabet of every word in a string. However, I am getting this Uncaught TypeError: Cannot read property 'split' of undefined error. Where ...
I created my own method which basically capitalizes the first alphabet of every word in a string. However, I am getting this Uncaught TypeError: Cannot read property 'split' of undefined error. Where ...
I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...
I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...
How do I add and remove a class only to selected radio button of a set of radio buttons? <table class="radioClass" role="set1"><tbody><tr><td><input type="radio" value=" ...
How do I add and remove a class only to selected radio button of a set of radio buttons? <table class="radioClass" role="set1"><tbody><tr><td><input type="radio" value=" ...
I am building an offline functionality for a Cordova app that I am developing and I have a php file that gets only the list of images and I have taken the JSON from it and saved in the client device ...
I am building an offline functionality for a Cordova app that I am developing and I have a php file that gets only the list of images and I have taken the JSON from it and saved in the client device ...