JavaScript Click event on page load on a Select tag click event page,click event on page load,click event

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:

HTML

<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>

Jquery

$(document).ready(function(){
  $("#lop").trigger("click");

  function myFunction(){
    alert("hi");
  }
});

JsFiddle

Answer:1

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>
Answer:2

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>
Answer:3

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

Answer:4

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>
Answer:5

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>
Answer:6

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>
Answer:7

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();
});
Answer:8

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 ...

  1. multi action button apk
  2. multi action button
  3. multi action button ux
  4. multi action button pro apk
  5. multi action home button pro apk
  6. multi action home button apk
  7. multi action home button
  8. multi action home button pro
  9. multi action home button pro 2.2.8 apk
  10. multi floating action button android
  11. multi action home button pro apk download
  12. multi action home button pro version download
  13. multi action home button apk download
  14. multi action home button app
  15. multi floating action button flutter
  16. multi action home button mod apk
  17. bootstrap multi action button
  18. multi action home button pro mod apk
  19. multi action home button download
  20. multi action home button full version

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 ...