How are we doing? Please help us improve Stack Overflow. Take our short survey

All Questions

Filter by
Sorted by
Tagged with
2896
votes
39answers
2.3m 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?
2405
votes
34answers
2.2m 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?
2324
votes
19answers
1.9m 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 ...
2166
votes
14answers
992k 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 ...
1537
votes
19answers
2.3m 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?
1397
votes
14answers
1.8m 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(); // ...
1269
votes
22answers
1.1m views

jQuery get specific option tag text

All right, say I have this: <select id='list'> <option value='1'>Option A</option> <option value='2'>Option B</option> <option value='3'>Option C</...
1099
votes
26answers
2.8m 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 ...
1085
votes
9answers
1.4m 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 ...
1068
votes
13answers
2.5m 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()); }); }) <...
1042
votes
24answers
747k 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::...
710
votes
6answers
502k 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 ...
687
votes
4answers
705k 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="...
684
votes
7answers
607k views

jQuery: Get selected element tag name

Is there an easy way to get a tag name? For example, if I am given $('a') into a function, I want to get 'a'.
673
votes
15answers
900k 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 ...
660
votes
13answers
648k views

document.getElementById vs jQuery $()

Is this: var contents = document.getElementById('contents'); The same as this: var contents = $('#contents'); Given that jQuery is loaded?
611
votes
10answers
456k 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 ...
596
votes
20answers
757k 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: <...
495
votes
11answers
295k 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?
485
votes
14answers
1.6m 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?
437
votes
19answers
769k 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.
421
votes
23answers
548k views

Toggle Checkboxes on/off

I have the following: $(document).ready(function() { $("#select-all-teammembers").click(function() { $("input[name=recipients\\[\\]]").attr('checked', true); }); }); ...
415
votes
9answers
298k 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?...
375
votes
16answers
821k 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=...
348
votes
6answers
240k 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 ...
335
votes
10answers
425k 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($(...
328
votes
6answers
248k 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 ...
297
votes
3answers
366k 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="...
291
votes
14answers
293k 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 ...
288
votes
4answers
177k 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 ...
285
votes
8answers
212k 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)...
264
votes
25answers
582k 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&...
261
votes
18answers
540k 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>...
257
votes
9answers
492k 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;...
256
votes
5answers
265k 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 ...
251
votes
8answers
405k 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")){ ...
241
votes
17answers
438k 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 ...
237
votes
3answers
539k 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-...
224
votes
4answers
196k 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'...
216
votes
4answers
178k 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="...
215
votes
6answers
150k 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 ...
212
votes
9answers
311k 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 ...
192
votes
5answers
107k 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[...
185
votes
12answers
295k 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>...
184
votes
9answers
142k views

jquery data selector

I need to select elements based on values stored in an element's .data() object. At a minimum, I'd like to select top-level data properties using selectors, perhaps like this: $('a').data("category","...
184
votes
6answers
303k 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 ...
178
votes
11answers
216k 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">...
178
votes
8answers
93k 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 ...
176
votes
14answers
165k 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....
175
votes
8answers
119k 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&...

1
2 3 4 5
160