SlideShare a Scribd company logo
1 of 8
JQUERY
SELECTION
OPTIMISATION
Review of Selection
There several ways of selecting
elements in jQuery, a few are listed
below

There several ways of selecting elements in
jQuery, a few are listed below:
<ul>

$(‘> p’)
$(‘li:not(‘li.selected’)’)
$(‘li:first’)
$(‘li.since ~ li’)
$(‘h2 + div’)

</ul>
Consider Selecting lists element after
‘li.selected’, we could simple use

e.t.c.

<li>First item</li>
<li class="selected">Second item</li>
<li>Third item</li>
<li>Fourth item</li>
<li>Fifth item</li>

$(‘li.selected’).nextAll(‘li’)
OR
$(‘li.selected ~ li’)
More Examples
Consider selecting all the elements of a
div as in

<div id=“content”>
…
...
…
</div>

From the example at the side,
Some of the methods that be used are
$(‘> *’,’#content’);
$(‘#content’).children()
$(‘#content’).find(‘> *’)
Question: Which is faster?
More Examples
// Implied universal selection.
$( ".category :radio" );
// Same thing, explicit now.

$( ".category *:radio" );
// Much better.
$( ".category input:radio" );

// Fast:
$( "#container div.alert " );
// Super-fast:
$( "#container" ).find( "div.alert" );
Selecting with Regular Expressions
Consider Selecting the ‘p’ tag which contains the word greetings

<p>Hello Emmanuel greetings</p>
<p>Hello Emmanuel, greetings to you</p>
<p>greetings to you Emmanuel</p>
<p>Hello Emmanuel</p>
var count = $('p').filter(function(){
return $(this).text().match(/(^|s)?.*greeting*/);
});
Advice
Use an ID if Possible (uniqueness)
$(‘#content’)
Avoid Selecting by Class Only (may fast on modern browsers but slow on old browsers)
$(‘.span12’)
KISS ( Keep it Simply Simple)
$("body #page:first-child article.main p#intro em");
OR
$("p#intro em");
$(‘p’).find(‘#into em’)

Increase Specificity from Left to Right
Avoid Selector Repetition
$("p").css({"color":"blue“,}).text();
$("p").css("font-size", "1.2em");
$("p").text("Text changed!");
Pitfalls to Avoid
The following terminates chaining
.get()
.text()
Contact:

Emmanuel.Adeyemi@novatiang.com

More Related Content

Viewers also liked

Tabloid Biru Edisi 1
Tabloid Biru Edisi 1Tabloid Biru Edisi 1
Tabloid Biru Edisi 1
tabloidbiru
 
Bilakaera ekonomikoa
Bilakaera ekonomikoaBilakaera ekonomikoa
Bilakaera ekonomikoa
Aniiita8
 
Secciones cónicas
Secciones cónicasSecciones cónicas
Secciones cónicas
fcernamuoz
 

Viewers also liked (19)

The Christmas Game
The Christmas GameThe Christmas Game
The Christmas Game
 
Tecnologia y comercio
Tecnologia y comercioTecnologia y comercio
Tecnologia y comercio
 
Educomics (Menggunakan komik ketika mengajar)
Educomics (Menggunakan komik ketika mengajar)Educomics (Menggunakan komik ketika mengajar)
Educomics (Menggunakan komik ketika mengajar)
 
hackathon2014_!ERROR
hackathon2014_!ERRORhackathon2014_!ERROR
hackathon2014_!ERROR
 
Interactive Board (Pengurusan IT)
Interactive Board (Pengurusan IT)Interactive Board (Pengurusan IT)
Interactive Board (Pengurusan IT)
 
Capital humano
Capital humanoCapital humano
Capital humano
 
Tabloid Biru Edisi 1
Tabloid Biru Edisi 1Tabloid Biru Edisi 1
Tabloid Biru Edisi 1
 
Bilakaera ekonomikoa
Bilakaera ekonomikoaBilakaera ekonomikoa
Bilakaera ekonomikoa
 
Secciones cónicas
Secciones cónicasSecciones cónicas
Secciones cónicas
 
Conceptos clave de la tecnología de internet
Conceptos clave de la tecnología de internetConceptos clave de la tecnología de internet
Conceptos clave de la tecnología de internet
 
каталог Lamark 2014
каталог Lamark 2014каталог Lamark 2014
каталог Lamark 2014
 
Exportaciones e importaciones de america latina
Exportaciones e importaciones de america latinaExportaciones e importaciones de america latina
Exportaciones e importaciones de america latina
 
BIMBO
BIMBOBIMBO
BIMBO
 
Art 98 Ley Aduanera
Art 98 Ley AduaneraArt 98 Ley Aduanera
Art 98 Ley Aduanera
 
SFI-Photogrammetry, NMHU
SFI-Photogrammetry, NMHUSFI-Photogrammetry, NMHU
SFI-Photogrammetry, NMHU
 
P resentation in health
P resentation in healthP resentation in health
P resentation in health
 
Composed instrumental music
Composed instrumental musicComposed instrumental music
Composed instrumental music
 
Philippine Popular Music
Philippine Popular MusicPhilippine Popular Music
Philippine Popular Music
 
Visual arts in Philippine drama and festivals ^_^
Visual arts in Philippine drama and festivals ^_^Visual arts in Philippine drama and festivals ^_^
Visual arts in Philippine drama and festivals ^_^
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Jquery selection optimisation

  • 2. Review of Selection There several ways of selecting elements in jQuery, a few are listed below There several ways of selecting elements in jQuery, a few are listed below: <ul> $(‘> p’) $(‘li:not(‘li.selected’)’) $(‘li:first’) $(‘li.since ~ li’) $(‘h2 + div’) </ul> Consider Selecting lists element after ‘li.selected’, we could simple use e.t.c. <li>First item</li> <li class="selected">Second item</li> <li>Third item</li> <li>Fourth item</li> <li>Fifth item</li> $(‘li.selected’).nextAll(‘li’) OR $(‘li.selected ~ li’)
  • 3. More Examples Consider selecting all the elements of a div as in <div id=“content”> … ... … </div> From the example at the side, Some of the methods that be used are $(‘> *’,’#content’); $(‘#content’).children() $(‘#content’).find(‘> *’) Question: Which is faster?
  • 4. More Examples // Implied universal selection. $( ".category :radio" ); // Same thing, explicit now. $( ".category *:radio" ); // Much better. $( ".category input:radio" ); // Fast: $( "#container div.alert " ); // Super-fast: $( "#container" ).find( "div.alert" );
  • 5. Selecting with Regular Expressions Consider Selecting the ‘p’ tag which contains the word greetings <p>Hello Emmanuel greetings</p> <p>Hello Emmanuel, greetings to you</p> <p>greetings to you Emmanuel</p> <p>Hello Emmanuel</p> var count = $('p').filter(function(){ return $(this).text().match(/(^|s)?.*greeting*/); });
  • 6. Advice Use an ID if Possible (uniqueness) $(‘#content’) Avoid Selecting by Class Only (may fast on modern browsers but slow on old browsers) $(‘.span12’) KISS ( Keep it Simply Simple) $("body #page:first-child article.main p#intro em"); OR $("p#intro em"); $(‘p’).find(‘#into em’) Increase Specificity from Left to Right Avoid Selector Repetition $("p").css({"color":"blue“,}).text(); $("p").css("font-size", "1.2em"); $("p").text("Text changed!");
  • 7. Pitfalls to Avoid The following terminates chaining .get() .text()