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.
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.
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.
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!')
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With