Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find elements with class and id

You can loop through all elements where id contains med_date Like this :

$( $('[id^=med_date_]') ).each(function() {

Can you do it by id and class, something like this

$( $('[id^=med_date_] [class^=med_group_]') ).each(function() {

The id can be med_date_1, med_date_2, med_date_3, etc
same for the class med_group_1, med_group_2, med_group_3, etc.

like image 277
morne Avatar asked Mar 09 '23 23:03

morne


2 Answers

Yes sure you've just to remove the space between the selectors like :

$('[id^=med_date_][class^=med_group_]')

That the selector for class AND id, if you want class OR id you could use comma separator :

$('[id^=med_date_],[class^=med_group_]')

Hope his helps.

$( $('[id^=med_date_][class^=med_group_]') ).each(function() {
   console.log($(this).text());  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span id='med_date_1' class='med_group_1'>Span 1</span>
<br>
<span id='med_date_2' class='med_group_2'>Span 2</span>
<br>
<span id='med_date_3' class='med_group_3'>Span 3</span>
like image 140
Zakaria Acharki Avatar answered Mar 15 '23 07:03

Zakaria Acharki


You were close. Yes, you can use multiple selectors, you need to remove space as it indicates Descendant Selector (“ancestor descendant”)

$('[id^=med_date_][class^=med_group_]').doSomething()

$('[id^=med_date_][class^=med_group_]').css('color', 'green')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='med_date_1' class='med_group_1'>Span 1</span>
<br>
<span id='med_date_2' class='med_group_2'>Span 2</span>
<br>
<span id='med_date_3' class='med_group_3'>Span 3</span>
like image 26
Satpal Avatar answered Mar 15 '23 06:03

Satpal