Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select div that ends with a class

Like this one:

<div class="abc foo-something">

How can I select div that has a class ending in -something?

like image 977
Ann Avatar asked Aug 12 '11 20:08

Ann


People also ask

How do I select a div by its class?

Use document.querySelector on to select a div and then select an element within it with the given class after that. We just call querySelector on the element with the ID mydiv to select items inside that div. Therefore, innerDiv is the div with the class myclass .

How do you select an element with a specific class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.

What selector would I use to query for all elements with an ID that ends with a particular string?

To get the elements with an ID that ends with a given string, use attribute selector with $ character.

How do you find the last element of a class?

To select the last element of a specific class, you can use the CSS :last-of-type pseudo-class. In this snippet, we'll show examples with the HTML <article> and <p> elements.


1 Answers

@Einacio commented correct that $("div[class$='-something']") will not work for <div class="foo-something abc">. To select and this case you can add a second selector, with the following result:

$("div[class$='-something'],div[class*='-something ']")

This will select and classes that ending with -something and a space follows.

Check this for more info.

like image 172
Sotiris Avatar answered Sep 30 '22 17:09

Sotiris