Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: select input with given name and value

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");
like image 699
user984003 Avatar asked Feb 18 '13 12:02

user984003


2 Answers

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);
like image 122
Adil Avatar answered Oct 15 '22 18:10

Adil


No need for the comma. Try:

 var checked = $('input[name = weekday][value =1]').attr("checked", "checked");
like image 22
Darren Avatar answered Oct 15 '22 17:10

Darren