I want to check the input that is named weekday and has value = 1. I tried the line below. It checks all weekdays.
$('input[name = weekday], [value =1]').attr("checked", "checked");
Do not use comma to apply both conditions on same element.
$('input[name=weekday][value=1]').attr("checked", "checked");
As a side note you should use prop() instead of attr()
for properties as suggested by jQuery doc and pointed by @tyleha.
As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.
You can use .prop( propertyName, value ) to set the checked property as shown below.
$('input[name=weekday][value=1]').prop("checked", true);
No need for the comma. Try:
var checked = $('input[name = weekday][value =1]').attr("checked", "checked");
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