Like this one:
<div class="abc foo-something">
How can I select div
that has a class ending in -something
?
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 .
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.
To get the elements with an ID that ends with a given string, use attribute selector with $ character.
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.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With