Found 766 Articles for JQuery

How to exclude first and second element from jQuery selector?

Amit D
Updated on 18-Dec-2019 07:43:21

627 Views

To exclude first and second element from jQuery, use the slice() method.ExampleYou can try to run the following code to exclude first and element from jQuery:Live Demo $(document).ready(function(){    $('p').slice(2).css('font-size', '24px'); }); Java C++ Ruby Groovy

How do we use .not() with jQuery selector?

Amit D
Updated on 18-Dec-2019 07:42:10

139 Views

The .not() is used to select all elements except an element, which can be specified.ExampleYou can try to run the following code to learn how to use .not()  with jQuery selector.Live Demo $(document).ready(function(){   $("p:not(.myclass)").css("background-color", "red"); }); Heading This is demo text. This is another text.

How to remove DOM elements in jQuery?

Amit D
Updated on 09-Dec-2019 06:30:41

379 Views

The empty( ) method remove all child nodes from the set of matched elements whereas the method remove( expr ) method removes all matched elements from the DOM.ExampleYou can try to run the following code to learn how to remove DOM elements in jQuery:Live Demo           jQuery Example                              $(document).ready(function() {             $("div").click(function () {                $(this).remove( );             });          });                              .div{ margin:10px;padding:12px; border:2px solid #666; width:60px;}                             Click on any square below:                                            

What are the best practices to improve jQuery selector performance?

Amit D
Updated on 09-Dec-2019 06:48:46

822 Views

To enhance the performance of jQuery selector, you need to perform optimization. Here are some of the techniques:Cache SelectorCaching enhances the performance of the application. Cache your jQuery selectors for better performance. This can be done using the ID as your selector. For example, this caches the selector and stores it in variable.var $elm = $("#elm");Instead of using the jQuery ID selector, use the $elm variable:var $elm = $("#elm"); $elm.addClass(‘example’);Use ID selectorjQuery is a JavaScript library. In JavaScript, document.getElementById() is used to select the HTML element, which is the fastest way. Since jQuery is written on top of JavaScript, it ... Read More

How to get a DOM Element from a jQuery Selector?

Amit D
Updated on 18-Dec-2019 07:53:30

717 Views

To get a DOM Element from a jQuery Selector, use the $('#element').get(0). This returns the DOM element.ExampleYou can try to run the following code to learn how to get a DOM Element from a jQuery Selector. For our checkbox example, get a DOM element like the following code:Live Demo $(document).ready(function(){    $(":checkbox").click(function() {      if ($(this).is(":checked")) {        alert("Checkbox checked!");      }   }); });  

How to write a jQuery selector for the label of a checkbox?

Amit D
Updated on 09-Dec-2019 06:16:03

2K+ Views

To write a jQuery selector for the label of a checkbox, use the for attribute of label element.ExampleYou can try to run the following code to learn how to write a jQuery selector for the label of a checkbox:Live Demo $(document).ready(function(){    $("label[for='mysubjects1']").css("background-color", "yellow");    $("label[for='mysubjects2']").css("background-color", "gray"); });     Subjects     New Subjects

How to use jQuery selectors on custom data attributes using HTML5?

Amit D
Updated on 18-Dec-2019 08:16:25

228 Views

To use jQuery selectors on custom data attributes, you can use contains, starts with, and ends with for the HTML data attributes.ExampleOther than that, you can try to run the following code to learn how to use jQuery selectors on custom data attributes using HTML5:Live Demo $(document).ready(function(){   //stored selector   var group = $('ol[data-group="Subjects"]');   // starts with M   var maths = $('[data-subject^="M"]',group).css('color','green');   // contains the string graph   var graph = $('[data-subject*="graph"]',group).css('color','blue'); });   Maths   Science   Geography   History

How to use wildcards like $ ('#name*'), $ ('#name%') in jQuery selectors?

Amit D
Updated on 13-Jun-2020 12:27:36

3K+ Views

For getting the id that begins or ends with a particular string in jQuery selectors, you shouldn’t use the wildcards $('#name*'), $('#name%'). Instead use the characters ^and $. The ^ is used is used to get all elements starting with a particular string. The $ is used is used to get all elements ending with a particular string.ExampleYou can try to run the following code to learn how to get id beginning and endingwith a particular string.Live Demo $(document).ready(function(){   $("[id$=new1]").css("background-color", "yellow");   $("[id^=myid]").css("background-color", "green"); }); Java HTML Ruby C++

How to use jQuery selector for the elements with an ID that ends with a given string?

Amit D
Updated on 18-Dec-2019 08:15:42

1K+ Views

To get the elements with an ID that ends with a given string, use attribute selector with $ character.ExampleYou can try to run the following code to learn how to to use jQuery selector for the elements with an ID that ends with a given string. Let’s say we are going for elements with an ID ending with the string “new1”.Live Demo $(document).ready(function(){   $("[id$=new]").css("background-color", "yellow");     }); Java HTML Ruby

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

David Meador
Updated on 18-Dec-2019 08:28:37

57 Views

To get the children of the $(this) selector in jQuery, use the find() method with each() method. Let us first see how to add jQuery:ExampleYou can try to run the following code to get the children of the $(this) selector:Live Demo           jQuery Example                      // Set the click handler on your div          $("body").off("click", "#mydiv").on("click", "#mydiv", function() {                      // Find the image using.find() and .each()            $(this).find("img").each(function() {                         var img = this;                      });          });                                #mydiv {             vertical-align: middle;             background-color: #Ffff76;             cursor: pointer;             padding: 20px;          }                                        

Advertisements