Kishore
Kishore

Reputation: 1912

jQuery selector for selecting an input field with a class

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.

Upvotes: 12

Views: 50699

Answers (4)

gen_Eric
gen_Eric

Reputation: 227240

$(':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.

Upvotes: 14

Kyle
Kyle

Reputation: 4449

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

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

Upvotes: 4

MikeM
MikeM

Reputation: 27405

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

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

Upvotes: 8

Joseph Marikle
Joseph Marikle

Reputation: 78520

$(':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

Upvotes: 7

Related Questions