Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - get element through id and then through class selector

I have something like this

<div id="div1">
   <span class="label">some text</span>
</div>
<div id="div2">
   <span class="label">some text</span>
</div>

I want to select a given span through id and then throu class. Something like #div1.label or #div2.span but this way doesn't work. How I can?

like image 604
BAD_SEED Avatar asked Sep 19 '12 14:09

BAD_SEED


2 Answers

Use a space to indicate the element is a child:

$('#div1 .label');
$('#div2 span');
$('#div2 span.label');

#div1.label would select an element with id div1 and a class of label.

like image 185
noj Avatar answered Nov 03 '22 01:11

noj


You could do

$("#div1 span.label")

if you use #div1.label you're actually selecting an element with id div1 AND having the class label.

like image 38
Claudio Redi Avatar answered Nov 03 '22 03:11

Claudio Redi