FAQ Database Discussion Community


How to extend a jQuery selector that is stored in a variable?

jquery,performance,jquery-selectors
I have a jQuery selector stored in a variable, like so: var myVariable = $( ".js-selector" ); I'm wondering if and how I can use this variable to select children of this element, instead of writing: $( ".js-selector .some-child" ); How do I do this? I'm also wondering if I...

Is it okay to store the result of a JQuery selector in a variable?

javascript,jquery,jquery-selectors
Developers I know tend to call the same JQuery selectors over and over instead of storing the result in a variable. They are consistent with this approach. For example, they do this: var propName = $(this).attr('data-inv-name'); var propValue = $(this).attr('data-inv-value'); Instead of this: var current = $(this); var propName =...

Get First Span of a
using the data-id

javascript,jquery,html,jquery-selectors,jquery-ui-draggable
I want to get the first span element of a div. This is how i get the div DOM element. helper: function(event,ui){ var dataId = $(this).data('id'); console.log($('[data-id="' + dataId + '"')); } NOTE Im using Draggable plugign from jquery-ui The console.log prints correct the div element, but now i want...

Hot to get the text of the selected option in jQuery?

jquery,html,jquery-selectors
td is a jQuery variable that references a table cell <td> element. This cell contains a <select> element which contains a number of <option> elements; each has a text and value. This code returns the value (not text) of the selected option. td.children().val(); I tried many ways to get the...

Concatenate CSS selectors

jquery,css,jquery-selectors,css-selectors
Is there a smarter way where I can use $self instead of using foo in $('.foo.bar')... ? It feels like the extra foo is redundent. I don't want to look through some selector I already found. $('.foo').on('click', function () { $self = $(this); $('.foo.bar').removeClass('bar'); // Works but feels wrong. //$self('bar').removeClass('bar');...

CSS or jQuery selctor? [closed]

jquery,html,css,jquery-selectors,css-selectors
Let's say there is the following HTML: <div id="test"> <span class="testSpan"></span> </div> There are two ways of selecting the span inside the #test div (I know I can select the span directly but this is not the point of the question): $('#test > .testSpan'); and $('#test').find('.testSpan'); Which of these ways...

Using the this keyword with jQuery selectors

jquery,jquery-selectors,this
Can someone please help me with the syntax of using the jQuery this keyword? Here is my code that works: var obj = jQuery.parseJSON(data); $('.example_infobox1').addClass(obj.gridlayout); $('.example_infobox1 .info-box').addClass(obj.boxcolor); $('.example_infobox1 .info-box-icon').addClass(obj.iconcolor); $('.example_infobox1 i').addClass(obj.icon); $('.example_infobox1 .info-box-text').html(obj.text); $('.example_infobox1 .info-box-number').html(obj.number); Here is the code that I am working on: var obj = jQuery.parseJSON(data);...

JQuery how to select parent but not children

jquery,jquery-selectors
Sorry for what is probably an easy question, but I haven't found a way to make this work yet. I'm new to JQuery and I've got some nested lists which I'm trying to add a click event to a parent list which will slideToggle the children lists, which I've got...

Add class to nth of an element found on full page instead within a parent

jquery,jquery-selectors,element,addclass
I'm trying to add classes to the "nth" of a particular element - regardless of where it falls on the page. A simplified version of my html is: <div class="row"> <div class="col-sm-12"><section class="subsection"></section></div> <div class="col-sm-12"><section class="subsection"></section></div> </div> <div class=“row”> <div class="col-sm-12"><section class="subsection"></section></div> <div class="col-sm-12"><section...

Jquery Change CSS of variable div

jquery,jquery-selectors
I have a table with a bunch of td with the classes markTD and they each have a different integer for the attribute value. I would like to click the button called mark and change the color of just 1 of the td. $('#mark').click(function(){ var h2 = parseInt($('#h2').attr('value'), 10); //h2...

jQuery selector