Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all elements whose ID starts and ends with specific strings?

In CSS, how can I select all <tr> elements where their id begins with section_ and ends with _dummy?

E.g. I'd like to select and apply styles to the following <tr>’s:

<tr id="section_5_1_dummy">     <td>...</td> </tr>  <tr id="section_5_2_dummy">     <td>...</td> </tr> 
like image 760
Danny Beckett Avatar asked Apr 15 '13 07:04

Danny Beckett


People also ask

Which selector selects elements with a specific ID attribute?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Which document method takes an ID as a string and returns the first element with the given ID?

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

How can you write starts with or ends with or contains for CSS selector?

The main purpose of using starts with (^), ends with ($) and contains (*) in CSS Selector is to locate the UI elements having the attribute values text which is dynamically changing at the beginning, middle or end.


1 Answers

The following CSS3 selector will do the job:

tr[id^="section_"][id$="_dummy"] {     height: 200px; } 

The ^ denotes what the id should begin with.

The $ denotes what the id should end with.


id itself can be replaced with another attribute, such as href, when applied to (for example) <a>:

a[href^="http://www.example.com/product_"][href$="/about"] {     background-color: red; } 
like image 92
Danny Beckett Avatar answered Oct 17 '22 03:10

Danny Beckett