Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Select multiple div elements by ID using startswith common string

Tags:

html

jquery

css

I want to set the same css for all of these div elements:

    <div id="Some_id_1"></div>
    <div id="Some_id_2"></div>
    <div id="Some_id_3"></div>

How can I do that? thanks.

like image 872
eomeroff Avatar asked Aug 25 '26 13:08

eomeroff


1 Answers

You should be able to use the jQuery Attribute Starts With Selector:

Usage:

$('[attribute^="value"]')

Example, fitting your needs:

$('div[id^="Some_id_"]').each(function(){
    $(this).css('property','value');
});

This would be useful for setting these properties dynamically, however if you are just setting them in a static sense, I would recommend just applying a class="your_class" to all of your divs.

A Working Demo can be found here :

Demo

like image 174
Rion Williams Avatar answered Aug 28 '26 02:08

Rion Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!