12

I have

<input type="text" class="required" value="XXX" name="abc">
<input type="hidden" class="required" value="XXX" name="abcd">
<input type="text" class="required" value="XXX" name="abcef">

i have many input fields like this.

i need a jQuery selector where i can select a text field whose class is required

right now to select textfield i use $(':input[type="text"]')

how do i also put a condition to select class="required."

EDIT

so say now i have the following input fields

<div id="validate_info">
<input type="text" class="required" value="XXX" name="abc">
<input type="hidden" class="required" value="XXX" name="abcd">
<input type="text" class="required" value="XXX" name="abcef">
<input type="password" class="required" value="XXX" name="abcefg">
<input type="text"  value="XXX" name="abcefgsa">
<input type="password" value="XXX" name="abcsaefg">
</div>

i need one selector that can select all type="text", type="password" which has class="required" i tried

$('#validate_info $("input.required").filter("[type="password"],[type="text"]")');

but this doesnt seem to work.

1
  • 1
    $("input.required").filter("[type="password"],[type="text"]") is not a selector string. It should be $("#validate_info input.required").filter("[type='password'],[type='text']") or $("input.required", "#validate_info").filter("[type='password'],[type='"text']"). Also, make sure you use the right quotes inside quotes. Commented Aug 8, 2011 at 18:18

4 Answers 4

14
$(':input.required[type="text"]')

EDIT:

This will be slightly faster:

$('input.required[type="text"]')

Note:

:input will get all form elements (<select>, <textarea>, etc.)

input will only get <input> elements, and is slightly faster.

Sign up to request clarification or add additional context in comments.

8 Comments

+1 - This is probably the fastest selector of all the answers.
Better if you use input instead of :input. Then jQuery can use browser methods.
now if i have another field of type password and i also need a selector for that how do i do that?
@Kishore: $(':input.required[type="password"]'), or whatever combination of selectors you want.
@Kishore: You have to use the multiple selector then: $('input.required[type="password"], input.required[type="password"]') or filter: $('input.required').filter('[type="password"],[type="password"]')
|
8
$('.required:input[type="text"]')

(also $('input.required[type="text"]'))

Comments

7
$(':input[type="text"][class="required"]')

That should work

http://jsfiddle.net/ck8wt/

Edit

lol ... I couldn't see the forest through the trees. :P I'll leave mine up too though just as an illustration of how many ways this can be done. XD

Comments

4

If you just add the class to your selector, it should do what you are looking for.

$('.required:input[type="text"]')

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.