0

I have this HTML markup:

<input type="text" name="name1" class="stat" value="">
<input type="checkbox" name="name1" value="">

<input type="text" name="name2" class="stat" value="">
<input type="checkbox" name="name2" value="">

<input type="text" name="namei" class="stat" value="">
<input type="checkbox" name="namei" value="">

where i = 1,2,3, ... n

Q: How to select checkbox with name2 ?

Is this correct way ?

$('input[name="name2"]').find('input[type="checkbox"]').attr("disabled");
6
  • try input[name="nam2"][type="checkbox"]. Commented Dec 18, 2013 at 7:43
  • $('input[name="name2"]').find will find the dedeceends, actually $('input[name="name2"]').filter(..) is ok Commented Dec 18, 2013 at 7:44
  • "Is this correct?", not tested yet? Commented Dec 18, 2013 at 7:54
  • @wared tested, but with bad result Commented Dec 18, 2013 at 7:56
  • So, this is not correct ;) Commented Dec 18, 2013 at 7:58

2 Answers 2

2

You can use multiple attribute selectors at the same time. Your find won't work because the checkbox is a sibling of the text input, not a child. Try this:

$('input[name="name2"][type="checkbox"]').prop("disabled");

Note that this will be very slow in performance. A better solution is to use a class or id attribute on the checkbox itself and select by that. Also, you should use prop to retrieve properties of an element.

Finally, if you want to set the disabled property, you need to provide a second parameter to the prop method:

$('input[name="name2"][type="checkbox"]').prop("disabled", true); // disables the element
Sign up to request clarification or add additional context in comments.

Comments

1

find() selects elements descending of the matched element.

Here, checkboxes and input boxes are siblings.

Try:

$('input[type="text"][name="name2"]').next().attr("disabled","disabled");

OR

$('input[type="checkbox"][name="name2"]').attr("disabled","disabled");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.