Why do functional pseudos such as :not() and :has() allow quoted arguments?

This isn’t specific to :not(…) and :has(…) selectors- actually, all pseudos in Sizzle allow for quoted arguments. The pattern for pseudos’ arguments is defined as: pseudos = “:(” + characterEncoding + “)(?:\\((?:([‘\”])((?:\\\\.|[^\\\\])*?)\\2|([^()[\\]]*|(?:(?:” + attributes + “)|[^:]|\\\\.)*|.*))\\)|)” Which can be found on line 91 of sizzle.js as of 831c9c48… Let’s add some indentation to that, to … Read more

How to convert the result of jQuery .find() function to an array?

The majority of jQuery methods returns a jQuery object, which can be accessed like it is an array (e.g. it has a .length attribute, elements can be accessed using the square bracket notation ([0]), and it supports some array methods (slice())). jQuery has a method called toArray() which can be used to convert the jQuery … Read more

jQuery selectors: select two elements, and all elements in between

You can do it like this: $(‘#id’).nextUntil(‘#id2’).andSelf().add(‘#id2′) It’s important to note that .nextUntil() does not include the element it’s going until, it stops with the one before it. To add that element, you need to call .add(selector) on the end. You also need a .andSelf() to include the first element Update August 2017 jQuery method … Read more

Jquery element+class selector performance

Modern browsers expose a very efficient getElementsByClassName() method that returns the elements having a given class. That’s why a single class selector is faster in your case. To elaborate on your examples: $(“.txtClass”) => getElementsByClassName() $(“#childDiv2 .txtClass”) => getElementById(), then getElementsByClassName() $(“#childDiv2 > .txtClass”) => getElementById(), then iterate over children and check class $(“input.txtClass”) => … Read more

How do you change the style of cell in a JQuery.DataTable?

Cool, I am happy to report that I was able to answer my own question! I just defined a CSS style (alignRight), and added the style to the column like so: <style media=”all” type=”text/css”> .alignRight { text-align: right; } </style> oTable = $(‘#example’).dataTable( { “aoColumns” : [ { sWidth: ‘40%’ }, { sWidth: ‘60%’, sClass: … Read more

jQuery :contains(), but to match an exact string

You can use filter for this: $(‘#id’).find(‘*’).filter(function() { return $(this).text() === ‘John’; }); Edit: Just as a performance note, if you happen to know more about the nature of what you’re searching (e.g., that the nodes are immediate children of #id), it would be more efficient to use something like .children() instead of .find(‘*’). Here’s … Read more

jQuery select ancestor

Try parent() for the immediate parent element. $(“.click-me”).click(function() { var ancestor = $(this).parent(); alert(ancestor) }); Or parents() for all matching ancestor elements. $(“.click-me”).click(function() { var ancestors = $(this).parents(“.some-ancestor”); alert(ancestors) }); Or closest() for the first closest matching element (either an ancestor or self). $(“.click-me”).click(function() { var ancestor = $(this).closest(“.some-ancestor”); alert(ancestor) }); The difference between parents() … Read more

Using jQuery to search a string of HTML

Let’s split this question into two parts. First: var html=”<html><head></head><body><div class=”bar”></div></body></html>”; console.log($(html).find(‘div’)); and var html=”<html><head></head><body><div><div class=”bar”></div></div></body></html>”; console.log($(html).find(‘div’)); do not work because according to the jQuery() docs: When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, we use the browser’s .innerHTML property to parse … Read more