Skip to content
CMO & CTO
CMO & CTO

Closing the Bridge Between Marketing and Technology, By Luis Fernandez

  • Digital Experience
    • Experience Strategy
    • Experience-Driven Commerce
    • Multi-Channel Experience
    • Personalization & Targeting
    • SEO & Performance
    • User Journey & Behavior
  • Marketing Technologies
    • Analytics & Measurement
    • Content Management Systems
    • Customer Data Platforms
    • Digital Asset Management
    • Marketing Automation
    • MarTech Stack & Strategy
    • Technology Buying & ROI
  • Software Engineering
    • Software Engineering
    • Software Architecture
    • General Software
    • Development Practices
    • Productivity & Workflow
    • Code
    • Engineering Management
    • Business of Software
    • Code
    • Digital Transformation
    • Systems Thinking
    • Technical Implementation
  • About
CMO & CTO

Closing the Bridge Between Marketing and Technology, By Luis Fernandez

jQuery Selectors that Read Like English

Posted on July 21, 2013 By Luis Fernandez
\n

Your jQuery can read like English. Let your selectors tell the story.

\n\n\n\n

Think in sentences when you write jQuery selectors

\n\n\n\n

We reach for jQuery every day, and with jQuery two landing this spring while many teams still ship on the one ten branch for old IE, selectors are back in the spotlight. The trick is to make them read like English. Start with a clear subject such as a list or a container, then add plain filters that match how you would say it out loud. Say what you want, then say where or how to find it. Use :contains, :visible, :has, and :not like adjectives. Your code becomes a sentence your future self can read without a second coffee.

\n\n\n\n
// Read it out loud:\n// in the comments list pick items that contain TODO and are visible\nvar $todoItems = $('ul.comments li:contains("TODO"):visible');
\n\n\n\n

Chain verbs to guide the reader step by step

\n\n\n\n

Selectors get easier when you anchor first then walk the page. Think of find, has, filter, not, first, and last as verbs. Start from a stable root like #app or a section, then narrow down in small steps that mirror your inner voice. Each step should answer a tiny question: which group, which ones inside, which ones stay. This style survives refactors because it tells a story rather than playing selector trivia. It also makes code reviews faster since the intent sits right next to the code.

\n\n\n\n
// In the app find articles that have images and link out\nvar $outboundArticles = $('#app')\n  .find('article.post')\n  .has('img')\n  .filter(function () {\n    return $(this).find('a[href^="http"]').length > 0;\n  })\n  .addClass('hasOutbound');
\n\n\n\n

Write for speed and sanity without clever tricks

\n\n\n\n

Readable selectors are not slow by default. The big wins come from anchoring early, caching what you reuse, and avoiding deep puzzles that look smart but age badly. Old IE still lives in a lot of cubicles, so prefer classes over heavy attribute puzzles when you can, and stash selections that repeat. Match behavior with event delegation that also reads like English. You listen up high, then describe the links you care about. This pairs well with Bootstrap buzz and the push to responsive work since markup shifts yet your sentences stay the same.

\n\n\n\n
// Listen on the document for clicks on nav links that open a new tab\n$(document).on('click', 'nav a[target="_blank"]', function (e) {\n  e.preventDefault();\n  var $link = $(this);\n  // say it: if the link is visible and not inside a disabled item, follow it\n  if ($link.is(':visible') && !$link.closest('.disabled').length) {\n    window.open($link.attr('href'));\n  }\n});
\n\n\n\n

Tip sheet you can say out loud: pick a clear starting point, use find for scope, use has and filter for meaning, sprinkle :first or :last only when you truly want the edge of a set, and reach for not to say what stays out. When you feel tempted to craft a single mega selector, split it into two or three lines that explain themselves. Your teammate who just wrestled with jQuery one nine changes will thank you. And your future self will too.

\n\n\n\n
// Another example that reads like English\n// from the feed pick even articles that do not have a summary\nvar $targets = $('#feed')\n  .find('article')\n  .even()\n  .not(':has(.summary)');
\n\n\n\n

Write jQuery you can read out loud tomorrow and you will ship faster today.

\n
General Software Software Engineering

Post navigation

Previous post
Next post
  • Digital Experience (94)
    • Experience Strategy (19)
    • Experience-Driven Commerce (5)
    • Multi-Channel Experience (9)
    • Personalization & Targeting (21)
    • SEO & Performance (10)
  • Marketing Technologies (92)
    • Analytics & Measurement (14)
    • Content Management Systems (45)
    • Customer Data Platforms (4)
    • Digital Asset Management (8)
    • Marketing Automation (6)
    • MarTech Stack & Strategy (10)
    • Technology Buying & ROI (3)
  • Software Engineering (310)
    • Business of Software (20)
    • Code (30)
    • Development Practices (52)
    • Digital Transformation (21)
    • Engineering Management (25)
    • General Software (82)
    • Productivity & Workflow (30)
    • Software Architecture (85)
    • Technical Implementation (23)
  • 2025 (12)
  • 2024 (8)
  • 2023 (18)
  • 2022 (13)
  • 2021 (3)
  • 2020 (8)
  • 2019 (8)
  • 2018 (23)
  • 2017 (17)
  • 2016 (40)
  • 2015 (37)
  • 2014 (25)
  • 2013 (28)
  • 2012 (24)
  • 2011 (30)
  • 2010 (42)
  • 2009 (25)
  • 2008 (13)
  • 2007 (33)
  • 2006 (26)

Ab Testing Adobe Adobe Analytics Adobe Target AEM agile-methodologies Analytics architecture-patterns CDP CMS coding-practices content-marketing Content Supply Chain Conversion Optimization Core Web Vitals customer-education Customer Data Platform Customer Experience Customer Journey DAM Data Layer Data Unification documentation DXP Individualization java Martech metrics mobile-development Mobile First Multichannel Omnichannel Personalization product-strategy project-management Responsive Design Search Engine Optimization Segmentation seo spring Targeting Tracking user-experience User Journey web-development

©2025 CMO & CTO | WordPress Theme by SuperbThemes