Questions tagged [jquery-selectors]

Selectors can be used in jQuery to match a set of elements in a document. Most CSS selectors are implemented, as well as a set of custom ones.

Filter by
Sorted by
Tagged with
3066 votes
39 answers
2.5m views

How can I know which radio button is selected via jQuery?

I have two radio buttons and want to post the value of the selected one. How can I get the value with jQuery? I can get all of them like this: $("form :radio") How do I know which one is selected?
juan's user avatar
  • 81.1k
2530 votes
35 answers
2.4m views

Get selected text from a drop-down list (select box) using jQuery

How can I get the selected text (not the selected value) from a drop-down list in jQuery?
haddar's user avatar
  • 25.5k
2414 votes
19 answers
2.0m views

How to get the children of the $(this) selector?

I have a layout similar to this: <div id="..."><img src="..."></div> and would like to use a jQuery selector to select the child img inside the div on click. To get the div, I've ...
Alex's user avatar
  • 26.6k
2282 votes
14 answers
1.1m views

How can I select an element with multiple classes in jQuery?

I want to select all the elements that have the two classes a and b. <element class="a b"> So, only the elements that have both classes. When I use $(".a, .b") it gives me the union, but I ...
Mladen's user avatar
  • 25.7k
1679 votes
20 answers
2.7m views

How can I get the ID of an element using jQuery?

<div id="test"></div> <script> $(document).ready(function() { alert($('#test').id); }); </script> Why doesn't the above work, and how should I do this?
fearofawhackplanet's user avatar
1538 votes
15 answers
2.3m views

How can I select an element by name with jQuery?

I have a table column I’m trying to expand and hide. jQuery seems to hide the <td> elements when I select it by class but not by the element’s name. For example: $(".bold").hide(); // ...
T.T.T.'s user avatar
  • 34k
1285 votes
23 answers
1.1m views

jQuery get specific option tag text

Suppose I have a drop-down list like: <select id='list'> <option value='1'>Option A</option> <option value='2'>Option B</option> <option value='3'>...
Paolo Bergantino's user avatar
1233 votes
29 answers
3.3m views

Set select option 'selected', by value

I have a select field with some options in it. Now I need to select one of those options with jQuery. But how can I do that when I only know the value of the option that must be selected? I have the ...
w00's user avatar
  • 26.5k
1166 votes
9 answers
1.6m views

jQuery how to find an element based on a data-attribute value?

I've got the following scenario: var el = 'li'; and there are 5 <li>'s on the page each with a data-slide=number attribute (number being 1,2,3,4,5 respectively). I now need to find the ...
Jannis's user avatar
  • 17.2k
1144 votes
28 answers
896k views

Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)

Is there any way to select/manipulate CSS pseudo-elements such as ::before and ::after (and the old version with one semi-colon) using jQuery? For example, my stylesheet has the following rule: .span::...
JBradwell's user avatar
  • 11.6k
1119 votes
13 answers
2.8m views

Get the value in an input text box

What are the ways to get and render an input value using jQuery? Here is one: $(document).ready(function() { $("#txt_name").keyup(function() { alert($(this).val()); }); }) <...
Bharanikumar's user avatar
  • 25.6k
768 votes
15 answers
1.1m views

jQuery to loop through elements with the same class

I have a load of divs with the class testimonial and I want to use jquery to loop through them to check for each div if a specific condition is true. If it is true, it should perform an action. Does ...
geoffs3310's user avatar
  • 13.9k
736 votes
6 answers
550k views

Wildcards in jQuery selectors

I'm trying to use a wildcard to get the id of all the elements whose id begin with "jander". I tried $('#jander*'), $('#jander%') but it doesn't work.. I know I can use classes of the elements to ...
tirenweb's user avatar
  • 31.3k
730 votes
4 answers
804k views

jQuery selectors on custom data attributes using HTML5

I would like to know what selectors are available for these data attributes that come with HTML5. Taking this piece of HTML as an example: <ul data-group="Companies"> <li data-company="...
Jose3d's user avatar
  • 9,239
691 votes
13 answers
742k views

document.getElementById vs jQuery $()

Is this: var contents = document.getElementById('contents'); The same as this: var contents = $('#contents'); Given that jQuery is loaded?
Phillip Senn's user avatar
  • 47.2k
646 votes
10 answers
497k views

jQuery selector regular expressions

I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with a jQuery selector. I have looked for this myself but have been unable to find information on ...
Joel Cunningham's user avatar
598 votes
21 answers
760k views

Testing if a checkbox is checked with jQuery

If the checkbox is checked, then I only need to get the value as 1; otherwise, I need to get it as 0. How do I do this using jQuery? $("#ans").val() will always give me one right in this case: <...
Rajeev's user avatar
  • 46k
545 votes
15 answers
1.9m views

How can I change CSS display none or block property using jQuery?

How can I change CSS display none or block property using jQuery?
DEVOPS's user avatar
  • 18.5k
515 votes
11 answers
316k views

How do you check if a selector matches something in jQuery? [duplicate]

In Mootools, I'd just run if ($('target')) { ... }. Does if ($('#target')) { ... } in jQuery work the same way?
One Crayon's user avatar
  • 19.2k
475 votes
21 answers
884k views

How to get all options of a select using jQuery?

How can I get all the options of a select through jQuery by passing on its ID? I am only looking to get their values, not the text.
Ali's user avatar
  • 265k
439 votes
24 answers
635k views

Toggle Checkboxes on/off

I have the following: $(document).ready(function() { $("#select-all-teammembers").click(function() { $("input[name=recipients\\[\\]]").attr('checked', true); }); }); ...
AnApprentice's user avatar
436 votes
9 answers
329k views

jQuery Selector: Id Ends With?

Is there a selector that I can query for elements with an ID that ends with a given string? Say I have a element with an id of ctl00$ContentBody$txtTitle. How can I get this by passing just txtTitle?...
Josh Stodola's user avatar
  • 82.1k
400 votes
17 answers
891k views

Set selected option of select box

I want to set a option that was selected previously to be displayed on page load. I tried it with the following code: $("#gate").val('Gateway 2'); with <select id="gate"> <option value=...
DarkLeafyGreen's user avatar
362 votes
6 answers
267k views

jQuery OR Selector?

I am wondering if there is a way to have "OR" logic in jQuery selectors. For example, I know an element is either a descendant of an element with class classA or classB, and I want to do something ...
Suan's user avatar
  • 35.9k
356 votes
11 answers
485k views

jQuery first child of "this"

I'm trying to pass "this" from a clicked span to a jQuery function that can then execute jQuery on that clicked element's first child. Can't seem to get it right... <p onclick="toggleSection($(...
macca1's user avatar
  • 9,631
340 votes
4 answers
461k views

Not class selector in jQuery

Is there a simple selector expression to not select elements with a specific class? <div class="first-foo" /> <div class="first-moo" /> <div class="first-koo" /> <div class="...
medihack's user avatar
  • 16.3k
335 votes
7 answers
259k views

What is fastest children() or find() in jQuery?

To select a child node in jQuery one can use children() but also find(). For example: $(this).children('.foo'); gives the same result as: $(this).find('.foo'); Now, which option is fastest or ...
bart's user avatar
  • 15.1k
310 votes
4 answers
197k views

jQuery select all except first

In jQuery how do I use a selector to access all but the first of an element? So in the following code only the second and third element would be accessed. I know I can access them manually but there ...
usertest's user avatar
  • 27.4k
303 votes
8 answers
224k views

How can I detect if a selector returns null?

What is the best way to detect if a jQuery-selector returns an empty object. If you do: alert($('#notAnElement')); you get [object Object], so the way I do it now is: alert($('#notAnElement').get(0)...
peirix's user avatar
  • 37.2k
297 votes
14 answers
306k views

jQuery delete all table rows except first

Using jQuery, how do I delete all rows in a table except the first? This is my first attempt at using index selectors. If I understand the examples correctly, the following should work: $(some ...
Ken Paul's user avatar
  • 5,725
286 votes
9 answers
563k views

Get a list of checked checkboxes in a div using jQuery

I want to get a list of names of checkboxes that are selected in a div with certain id. How would I do that using jQuery? E.g., for this div I want to get array ["c_n_0"; "c_n_3"] or a string "c_n_0;...
Juozas Kontvainis's user avatar
282 votes
20 answers
662k views

jQuery: Check if div with certain class name exists

Using jQuery I'm programmatically generating a bunch of div's like this: <div class="mydivclass" id="myid1">Some Text1</div> <div class="mydivclass" id="myid2">Some Text2</div>...
avatar's user avatar
  • 12.2k
278 votes
9 answers
477k views

How to check if an element does NOT have a specific class?

How do I check if there isn't a class. For example, I know how to check to see if it has the class "test", but how do I check to see if it doesn't have the class "test"? if($(this).hasClass("test")){ ...
user1005793's user avatar
  • 2,891
272 votes
25 answers
607k views

jQuery Set Select Index

I have an select box: <select id="selectBox"> <option value="0">Number 0</option> <option value="1">Number 1</option> <option value="2">Number 2</option&...
user avatar
266 votes
19 answers
527k views

Using jquery to get all checked checkboxes with a certain class name

I know I can get all checked checkboxes on a page using this: $('input[type=checkbox]').each(function () { var sThisVal = (this.checked ? $(this).val() : ""); }); But I am now using this on a ...
leora's user avatar
  • 193k
266 votes
5 answers
288k views

jQuery selector for the label of a checkbox

<input type="checkbox" name="filter" id="comedyclubs"/> <label for="comedyclubs">Comedy Clubs</label> If I have a check box with a label describing it, how can I select the label ...
Darwyn's user avatar
  • 4,756
259 votes
3 answers
614k views

Access the css ":after" selector with jQuery [duplicate]

I have the following css: .pageMenu .active::after { content: ''; margin-top: -6px; display: inline-block; width: 0px; height: 0px; border-top: 14px solid white; border-...
dmr's user avatar
  • 22.1k
231 votes
9 answers
392k views

Get second child using jQuery

$(t).html() returns <td>test1</td><td>test2</td> I want to retrieve the second td from the $(t) object. I searched for a solution but nothing worked for me. Any idea how to ...
DarkLeafyGreen's user avatar
227 votes
4 answers
196k views

How can I exclude $(this) from a jQuery selector?

I have something like this: <div class="content"> <a href="#">A</a> </div> <div class="content"> <a href="#">B</a> </div> <div class="...
Logan Serman's user avatar
  • 29.7k
226 votes
4 answers
200k views

Selecting multiple classes with jQuery

I’ve had a good look and can’t seem to find out how to select all elements matching certain classes in one jQuery selector statement such as this: $('.myClass', '.myOtherClass').removeClass('theclass'...
Kieran Senior's user avatar
225 votes
6 answers
162k views

jQuery’s .bind() vs. .on()

I found two great articles talking about the new function .on(): jquery4u.com, elijahmanor.com. Is there any way where the .bind() still is better to use than .on()? For example, I have a sample ...
Samuel's user avatar
  • 12.2k
214 votes
11 answers
293k views

Changing the child element's CSS when the parent is hovered

First of all, I'm assuming this is too complex for CSS3, but if there's a solution in there somewhere, I'd love to go with that instead. The HTML is pretty straightforward. <div class="parent">...
Hartley Brody's user avatar
198 votes
5 answers
116k views

jQuery selector for inputs with square brackets in the name attribute

I'm trying to select this element which has square brackets in the name attribute: <input type="text" name="inputName[]" value="someValue"> I've tried this (which doesn't work): $('input[...
aidan's user avatar
  • 9,446
193 votes
4 answers
173k views

Combining a class selector and an attribute selector with jQuery

Is it possible to combine both a class selector and an attribute selector with jQuery? For example, given the following HTML: <TABLE> <TR class="myclass" reference="12345"><TD>...
Jason Child's user avatar
  • 1,997
189 votes
6 answers
341k views

jQuery: select an element's class and id at the same time?

I've got some links that I want to select class and id at the same time. This is because I've got 2 different behaviours. When a class of links got one class name they behave in one way, when the ...
ajsie's user avatar
  • 78.8k
189 votes
8 answers
97k views

How do I get jQuery to select elements with a . (period) in their ID?

Given the following classes and controller action method: public School { public Int32 ID { get; set; } publig String Name { get; set; } public Address Address { get; set; } } public class ...
Doug Wilson's user avatar
  • 4,263
188 votes
15 answers
190k views

jQuery hasClass() - check for more than one class

With: if(element.hasClass("class")) I can check for one class, but is there an easy way to check whether "element" has any of many classes? I am using: if(element.hasClass("class") || element....
Björn's user avatar
  • 13k
188 votes
7 answers
489k views

How to get all child inputs of a div element (jQuery)

HTML: <div id="panel"> <table> <tr> <td><input id="Search_NazovProjektu" type="text" value="" /></td> </tr> <tr> <td>&...
user137348's user avatar
  • 10.3k
188 votes
12 answers
303k views

Count immediate child div elements using jQuery

I have the following HTML node structure: <div id="foo"> <div id="bar"></div> <div id="baz"> <div id="biz"></div> </div> <span></span>...
Dónal's user avatar
  • 186k
186 votes
8 answers
141k views

Select element by exact match of its content

All right, I wonder if there is a way to make the :contains() jQuery's selector to select elements with only the string that is typed in for example - <p>hello</p> <p>hello world&...
julian's user avatar
  • 4,654

1
2 3 4 5
181