How can I select a switch using javascript or jquery?

I know this is probably easy, but I just can't figure it out.

I am working with a solution for the SAAS basket, so I canโ€™t customize the form code or use PHP, so I need to use javascript or jquery.

In this cart I have a form ....

<form method="post" action="cart.php" id="productDetailsAddToCartForm"> Please Select a Bottle Size: <ul> <li><label><input name="variation[1]" type="radio" class="RadioButton" value="30" /> 11oz</label></li> <li><label><input name="variation[1]" type="radio" class="RadioButton" value="31" /> 33oz </label> </li> </ul> </form> 

And I need to pre-select the first switch using jquery or javascript.

You can help?

+4
source share
2 answers

You can use attr with checked as follows:

 $('.RadioButton:first').attr('checked', true); 

or

 $('.RadioButton:first').attr('checked', 'checked'); 

Note that :first will select the first switch.

+4
source
 $(':input:radio:eq(0)').attr('checked', 'checked'); 

This will check the first field on the page.

+6
source

Source: https://habr.com/ru/post/1333718/


All Articles