Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:49:24+00:00 2026-05-29T08:49:24+00:00

How could I test CSS1-3 selectors to check that they get the correct elements,

  • 0

How could I test CSS1-3 selectors to check that they get the correct elements, e.g. with JavaScript (maybe jQuery)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:49:25+00:00Added an answer on May 29, 2026 at 8:49 am

    The simplest traditional way by far is to not use JavaScript at all, and just set up a test page by hand where you can test selectors to your heart’s content. The test cases you see on the Web (like the well-known CSS3.info Selectors Test) are really just souped-up versions hosted online.

    But if you’re looking for a JavaScript method, you can try the Selectors API. It’s available in modern DOM implementations (IE8+ and others) and it provides a JavaScript frontend for querying the DOM for element nodes using CSS selectors, as well as testing CSS selectors natively supported by a given browser.

    (For browsers that don’t implement the Selectors API, you’ll have to rely on jQuery, but remember that it provides support for a different set of selectors than what a browser supports as well as its own non-standard extensions which aren’t found in the Selectors spec. An example of using jQuery with Chrome’s JavaScript console to test a selector can be found here.)

    Call querySelector() or querySelectorAll() depending on what you want to test, and check the return value (preferably in your browser’s developer tools since you’re just testing):

    • If matches are found, the former method returns the first Element matched while the latter returns all elements matched as a NodeList.

    • If nothing is found, the former returns null while the latter returns an empty NodeList.

    • If the selector is invalid, an exception will be thrown which you can catch.

    Here are some examples with the command editor (multiline) in Firebug’s console on Firefox 10, tested on this very question:

    • Finding the first h1 in body:

      var h1 = document.body.querySelector('h1');
      console.log(h1);
      
      <h1 itemprop="name">
      
    • Querying descendants of that h1 element we just found:

      var subnodes = h1.querySelectorAll('*');
      console.log(subnodes[0]);
      
      <a class="question-hyperlink" href="/questions/9165859/how-do-i-test-css-selectors-in-javascript">
      
    • Testing the :-moz-any() pseudo-class in Firefox (:-webkit-any() in Safari/Chrome):

      // This selector works identically to h1 > a, li > a
      var hyperlinks = document.querySelectorAll(':-moz-any(h1, li) > a');
      console.log(hyperlinks);
      
      [a#nav-questions /questions, a#nav-tags /tags, a#nav-users /users, a#nav-badges /badges, a#nav-unanswered /unanswered, a#nav-askquestion /questions/ask, a.question-hyperlink /questio...vascript]
      
    • Testing a nonexistent selector (that perhaps many of us wish did exist):

      // :first-of-class doesn't exist!
      var selector = 'div.answer:first-of-class';
      
      try {
          var firstAnswer = document.querySelector(selector);
          console.log(firstAnswer);
      } catch (e) {
          console.log('Invalid selector: ' + selector);
      }
      
      Invalid selector: div.answer:first-of-class
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulty with the jQuery ajax success handler. Any javascript run-time errors that
I recently bought an iPod so that I could test my own HTML5 games
I need to test an ASP.NET site that requires SSL locally. I think IIS7
I'm on windows 7. Is there any way I could test the IE6-tuned code
Here's an integration test I wrote for a class that interacts with a database:
I am currently learning ARM Assembly and I was wandering whether I could test
Was thinking I could test the various answers I got in my question about
Before iOS5 I could test to see what URL I was getting a response
An ideal one would be where I could: Test my JS live, not just
I wonder if I could test Significant-Change location service (startMonitoringSignificantLocationChanges method ) in Xcode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.