Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When clicking on on the LI then automatically click on input radio

When you click on li, I want input radio to be clicked.

However, I am getting an error from conole log saying:

Uncaught RangeError: Maximum call stack size exceeded

How to fix this?

Here the html code:

    <ul class="Method">
        <li class="shipping_today active">
           <label> Label 1 </label>
            <input value="shipping_today"  name="shipping" type="radio" />
        </li>
        <li class="shipping_next_month">
            <label> Label 2 </label>
            <input value="shipping_next_month"  name="shipping" type="radio" />
        </li>
    </ul>

Jquery:

$(".Method li").click(function() { 
    var thisLi = $(this);
    var radio = $(this).find("input:radio");

    if (radio.val() == "shipping_today") {
        $(".Method li").eq(1).removeClass("active");
        $(this).addClass("active");
    }

    if (radio.val() == "shipping_next_month") {
        $(".Method li").eq(-2).removeClass("active");
        $(this).addClass("active");
    }

    radio.click(); //problem here...
});

Is my jQuery code good? what can be improved?

thanks.

like image 362
I'll-Be-Back Avatar asked Oct 16 '11 12:10

I'll-Be-Back


People also ask

How do you select a radio button when clicking text?

To select a radio button by clicking on its text in React:Add a label element for each radio button. The htmlFor prop of each label should be set to the id of each radio button. Click on the label element to select the radio button.

How do you make a radio button clickable in HTML?

To label a radio button, add a <label> element after the <input> element and insert a for attribute with the same value as the id of the associated <input> element. Then, write your label text in the <label> tag.

How do you make a radio button not clickable in HTML?

Answer: To make a radio button not selectable, in the button's INPUT tag you can use an onclick event handler like this: <INPUT type="radio" name="myButton" value="theValue" onclick="this. checked=false; alert('Sorry, this option is not available!')

How do you checked a radio button when a button is clicked?

One solution is to assign on mousedown the value you are going to assign to it (the opposite of what it has) in a variable on the node like this. __rval and check for its existence in your onclick handler. If it exists, you know the value in it is correct, though the this.


1 Answers

That's an infinite loop, because the click event you trigger on the radio button bubbles up to the <li> element and causes your handler to run recursively.

One solution would be to only relay the click event if its doesn't come from the radio button itself:

$(".Method li").click(function(event) {
    var thisLi = $(this);
    var radio = $(this).find("input:radio");

    // [...]

    if (radio[0] !== event.target) {
        radio.click();
    }
});

If you only want to check the radio button, however, relaying the event is not necessary. You can use prop() instead:

radio.prop("checked", true);
like image 57
Frédéric Hamidi Avatar answered Sep 21 '22 04:09

Frédéric Hamidi