0

how can i trigger a function in jquery option if option is already selected, i want to perform ajax function through it but control is not going to the jquery function

jquery file:-

$(document).ready(function(){
    $("#value").change(function(){
        alert("The text has been changed.");
     });
});

html where i want to triger a jquery function i click on selected option file:--

<select id="value">
    <option value="o"> SELECT-0</option>
    <option value="1" selected=selected> SELECT-1</option>
    <option value="2"> SELECT-2</option>
    <option value="3"> SELECT-3</option>
</select>
Nick
  • 565
  • 4
  • 19
Akshay Kumar
  • 5,740
  • 2
  • 12
  • 19

1 Answers1

2

You need to trigger() the change function on document.ready

For Comment:when the selected option is clicked again,. so add event of click

$(document).ready(function() {
  $("#value").on('change click',function() {
    alert("The text has been changed.");
  });
  $("#value").trigger('change')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<select id="value">
  <option value="o"> SELECT-0</option>
  <option value="1" selected=selected> SELECT-1</option>
  <option value="2"> SELECT-2</option>
  <option value="3"> SELECT-3</option>
</select>
prasanth
  • 22,145
  • 4
  • 29
  • 53