0

I am using select box in which I have two options. When I click on one option named "Reposition cover" it calls the JavaScript function. After function calling, it displays the "Reposition cover" in select box (default behavior of select box), but I want to display the default value ('Change' in my case) in select box once the function is called. And when the user will again click on "Reposition cover" option, the function will again call and select box will return it's default value and so on. Following is my code that I wrote to achieve this:

Html

<select id="cars">
  <option value="change" selected="selected">Change</option>
  <option value="rep">Reposition cover</option>
    <div>Reposition cover</div>

</select>

Javascript

 <script type="text/javascript">

     $(document).ready(function(){
     e1 = document.getElementById('cars');
    if(e1)
    {
        e1.addEventListener('change', function() {
        if(this.value == 'rep'){
         repositionCover();
          /*Execute your script */
        }
        else
        {
    
            }
    });
        }
     
     
    });
     </script>

  
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

Just reset the value of the select

$(document).ready(function(){
  $('#cars').on('change', function() {
    if(this.value == 'rep'){
      this.value = 'change';
      //repositionCover();
    } else {
      
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="cars">
  <option value="change" selected="selected">Change</option>
  <option value="rep">Reposition cover</option>
  <div>Reposition cover</div>

</select>
adeneo
  • 312,895
  • 29
  • 395
  • 388