0

I need to simulate a mouse click on a radio button,but the button is only selected. How do I make functions work for a case?

$("input[value='0']").click();

$('input[name=level]').on('click', function(event) {
  switch ($(this).val()) {
    case '0':
      alert("zero");
      break;
    case '1':
      alert("one");
      break;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<label>
1<input type="radio" class="option-input radio" checked value="1" name="level" />
0<input type="radio" class="option-input radio"  value="0" name="level" />
</label>
WEB_D
  • 69
  • 7

1 Answers1

4

Place the trigger on the element below your click function. You were trying to trigger a click on a function that has yet to be defined within the DOM.

$('input[name=level]').on('click', function(event) {
  switch ($(this).val()) {
    case '0':
      alert("zero");
      break;
    case '1':
      alert("one");
      break;
  }
});

$("input[value=0]").trigger('click');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
1<input type="radio" class="option-input radio" checked value="1" name="level" />
0<input type="radio" class="option-input radio"  value="0" name="level" />
</label>
dale landry
  • 7,831
  • 2
  • 16
  • 28