SlideShare a Scribd company logo
1 of 8
Author: Chandra Shekher
© chandrashekher
It’s very important to understand how to write
efficient element selection statement. One has
to be very careful while using jQuery selector
statement. Here we are going to discuss some
best way to use jQuery Selectors with example
 You can use ID as selector in jQuery. See below jQuery code.
$("#elmID");
 When IDs are used as selector then jQuery internally makes a
call to getElementById() method of Java script which directly
maps to the element.
 When Classes are used as selector then jQuery has to do DOM
traversal. So when DOM traversal is performed via jQuery
takes more time to select elements. In terms of speed and
performance, it is best practice to use IDs as selector.
 You can use CSS classes as selector. For example, to select
elements with "myCSSClass" following jQuery code can be
used.
$(".myCSSClass");
 As said earlier, when classes are used DOM traversal happens.
But there could be a situation where you need to use classes as
selector. For better performance, you can use tag name with the
class name. See below
$("div.myCSSClass");
 Above jQuery code, restricts the search element specific to
DIV elements only.
 Avoid complex selectors.
$("body .main p#myID em");
 Instead of using such a complex selector, we can simplify it.
$("p#myID em");
 The selectors are used thrice for 3 different operation.
$("#myID").css("color", "red");
$("#myID").css("font", "Arial");
$("#myID").text("Error occurred!");
The problem with above code is, jQuery has to traverse 3 times as
there are 3 different statements.
 But above statement can be combined into a single statement.
$("p").css({ "color": "red", "font": "Arial"}).text("Error occurred!");
 Your last selectors is always executed first.
For example, in below jQuery code, jQuery will first find all
the elements with class ".myCssClass" and after that it will
reject all the other elements which are not in "p#elmID".
$("p#elmID .myCssClass");
First Selected
Second Selected
Thankyou

More Related Content

What's hot

Intro to Javascript and jQuery
Intro to Javascript and jQueryIntro to Javascript and jQuery
Intro to Javascript and jQuery
Shawn Calvert
 
JS Libraries and jQuery Overview
JS Libraries and jQuery OverviewJS Libraries and jQuery Overview
JS Libraries and jQuery Overview
Aleksandr Motsjonov
 

What's hot (20)

jQuery Performance Tips and Tricks
jQuery Performance Tips and TricksjQuery Performance Tips and Tricks
jQuery Performance Tips and Tricks
 
jQuery Best Practice
jQuery Best Practice jQuery Best Practice
jQuery Best Practice
 
Locators in selenium - BNT 09
Locators in selenium - BNT 09Locators in selenium - BNT 09
Locators in selenium - BNT 09
 
Java script
Java scriptJava script
Java script
 
Learn css3
Learn css3Learn css3
Learn css3
 
Java script
Java scriptJava script
Java script
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
 
Intro to Javascript and jQuery
Intro to Javascript and jQueryIntro to Javascript and jQuery
Intro to Javascript and jQuery
 
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web TechnologyInternet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
 
Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2
 
J Query
J QueryJ Query
J Query
 
jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery Presentation
 
Javascript by geetanjali
Javascript by geetanjaliJavascript by geetanjali
Javascript by geetanjali
 
22 j query1
22 j query122 j query1
22 j query1
 
Javascript tutorial
Javascript tutorialJavascript tutorial
Javascript tutorial
 
Intro to jQuery
Intro to jQueryIntro to jQuery
Intro to jQuery
 
Lecture 5: Client Side Programming 1
Lecture 5: Client Side Programming 1Lecture 5: Client Side Programming 1
Lecture 5: Client Side Programming 1
 
How to use CSS Selector to identify Web Elements for selenium scripts
How to use CSS Selector to identify Web Elements for selenium scriptsHow to use CSS Selector to identify Web Elements for selenium scripts
How to use CSS Selector to identify Web Elements for selenium scripts
 
Introuction To jQuery
Introuction To jQueryIntrouction To jQuery
Introuction To jQuery
 
JS Libraries and jQuery Overview
JS Libraries and jQuery OverviewJS Libraries and jQuery Overview
JS Libraries and jQuery Overview
 

Viewers also liked

Jquery14
Jquery14Jquery14
Jquery14
k plus
 
Introduction to j query
Introduction to j queryIntroduction to j query
Introduction to j query
thewarlog
 

Viewers also liked (20)

Jquery14
Jquery14Jquery14
Jquery14
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
Introducing jQuery
Introducing jQueryIntroducing jQuery
Introducing jQuery
 
jQuery Effects
jQuery EffectsjQuery Effects
jQuery Effects
 
Introductory css and j query
Introductory css and j queryIntroductory css and j query
Introductory css and j query
 
jQuery Beginner
jQuery BeginnerjQuery Beginner
jQuery Beginner
 
jQuery Conference 2010 - Getting Involved
jQuery Conference 2010 - Getting InvolvedjQuery Conference 2010 - Getting Involved
jQuery Conference 2010 - Getting Involved
 
JAVASCRIPT and JQUERY For Beginner
JAVASCRIPT and JQUERY  For BeginnerJAVASCRIPT and JQUERY  For Beginner
JAVASCRIPT and JQUERY For Beginner
 
Learn JS concepts by implementing jQuery
Learn JS concepts by implementing jQueryLearn JS concepts by implementing jQuery
Learn JS concepts by implementing jQuery
 
J query 17-visual-cheat-sheet
J query 17-visual-cheat-sheetJ query 17-visual-cheat-sheet
J query 17-visual-cheat-sheet
 
webstudy jquery
webstudy jquerywebstudy jquery
webstudy jquery
 
JQuery: JavaScript Library of the Future
JQuery: JavaScript Library of the FutureJQuery: JavaScript Library of the Future
JQuery: JavaScript Library of the Future
 
Jquery
JqueryJquery
Jquery
 
Introduction to j query
Introduction to j queryIntroduction to j query
Introduction to j query
 
WordCamp London 2013
WordCamp London 2013WordCamp London 2013
WordCamp London 2013
 
Beginning jQuery
Beginning jQueryBeginning jQuery
Beginning jQuery
 
Intro to jQuery - Tulsa Ruby Group
Intro to jQuery - Tulsa Ruby GroupIntro to jQuery - Tulsa Ruby Group
Intro to jQuery - Tulsa Ruby Group
 
JQuery Comprehensive Overview
JQuery Comprehensive OverviewJQuery Comprehensive Overview
JQuery Comprehensive Overview
 
JavaScript the Smart Way - Getting Started with jQuery
JavaScript the Smart Way - Getting Started with jQueryJavaScript the Smart Way - Getting Started with jQuery
JavaScript the Smart Way - Getting Started with jQuery
 
Intro to jquery
Intro to jqueryIntro to jquery
Intro to jquery
 

Similar to Efficient use of jQuery selector

J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
 
CSS Methodology
CSS MethodologyCSS Methodology
CSS Methodology
Zohar Arad
 

Similar to Efficient use of jQuery selector (20)

J query1
J query1J query1
J query1
 
Styling Components with JavaScript: MelbCSS Edition
Styling Components with JavaScript: MelbCSS EditionStyling Components with JavaScript: MelbCSS Edition
Styling Components with JavaScript: MelbCSS Edition
 
jQuery
jQueryjQuery
jQuery
 
Intro to jQuery
Intro to jQueryIntro to jQuery
Intro to jQuery
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Many
 
Jquery Basics
Jquery BasicsJquery Basics
Jquery Basics
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
 
Understanding CSS Selectors in Selenium.pdf
Understanding CSS Selectors in Selenium.pdfUnderstanding CSS Selectors in Selenium.pdf
Understanding CSS Selectors in Selenium.pdf
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Don't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQueryDon't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQuery
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
 
Web Development Course - JQuery by RSOLUTIONS
Web Development Course - JQuery by RSOLUTIONSWeb Development Course - JQuery by RSOLUTIONS
Web Development Course - JQuery by RSOLUTIONS
 
CSS Methodology
CSS MethodologyCSS Methodology
CSS Methodology
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
Jqueryppt (1)
Jqueryppt (1)Jqueryppt (1)
Jqueryppt (1)
 
Jquery
JqueryJquery
Jquery
 
Jquery News Packages
Jquery News PackagesJquery News Packages
Jquery News Packages
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 

Efficient use of jQuery selector

  • 1. Author: Chandra Shekher © chandrashekher
  • 2. It’s very important to understand how to write efficient element selection statement. One has to be very careful while using jQuery selector statement. Here we are going to discuss some best way to use jQuery Selectors with example
  • 3.  You can use ID as selector in jQuery. See below jQuery code. $("#elmID");  When IDs are used as selector then jQuery internally makes a call to getElementById() method of Java script which directly maps to the element.  When Classes are used as selector then jQuery has to do DOM traversal. So when DOM traversal is performed via jQuery takes more time to select elements. In terms of speed and performance, it is best practice to use IDs as selector.
  • 4.  You can use CSS classes as selector. For example, to select elements with "myCSSClass" following jQuery code can be used. $(".myCSSClass");  As said earlier, when classes are used DOM traversal happens. But there could be a situation where you need to use classes as selector. For better performance, you can use tag name with the class name. See below $("div.myCSSClass");  Above jQuery code, restricts the search element specific to DIV elements only.
  • 5.  Avoid complex selectors. $("body .main p#myID em");  Instead of using such a complex selector, we can simplify it. $("p#myID em");
  • 6.  The selectors are used thrice for 3 different operation. $("#myID").css("color", "red"); $("#myID").css("font", "Arial"); $("#myID").text("Error occurred!"); The problem with above code is, jQuery has to traverse 3 times as there are 3 different statements.  But above statement can be combined into a single statement. $("p").css({ "color": "red", "font": "Arial"}).text("Error occurred!");
  • 7.  Your last selectors is always executed first. For example, in below jQuery code, jQuery will first find all the elements with class ".myCssClass" and after that it will reject all the other elements which are not in "p#elmID". $("p#elmID .myCssClass"); First Selected Second Selected