jqueryjquery-selectorscss-selectors

JQuery Selectors: How to select item with specific class *and* id


I'm trying to select an HTML element on my page that has a specific class and ID. Here's the tag:

<div class="statusLight" id="green"></div>

I tried this with no luck:

$statusLight = $('.statusLight#green');

I know that I could simply say

$statusLight = $('#green');

But I was trying to find a way to select it based on its class as well. Any help would be appreciated.


Solution

  • Both #green.statusLight and .statusLight#green are valid selectors and should select element you're looking for. First one will be faster though.

    Are you using $(...) after your document is loaded, i.e. from $(document).ready(function() { ... }) or by placing your script after your element?