Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 542
Kishore Avatar asked Aug 08 '11 16:08

Kishore


People also ask

How do you target a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

Which selector does jQuery use to select?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.

Can you use CSS selectors in jQuery for selecting elements?

jQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element. name − The name of the property to access.


4 Answers

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

like image 170
Rocket Hazmat Avatar answered Oct 19 '22 23:10

Rocket Hazmat


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

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

like image 21
MikeM Avatar answered Oct 19 '22 23:10

MikeM


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

like image 40
Joseph Marikle Avatar answered Oct 20 '22 00:10

Joseph Marikle


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

$('.required:input[type="text"]')
like image 29
Kyle Avatar answered Oct 20 '22 00:10

Kyle