seantomburke
seantomburke

Reputation: 11712

jQuery Selector equivalent for ":not"

I want to achieve desired output below with pure JavaScript since jQuery isn't available on the page where I am executing code.

HTML:

<div id="firstDiv">
    <b>Item 1:</b> tacos
</div>

Output:

tacos

How can I achieve this?

My failed attempt with jQuery:

$("#firstDiv:not('b')").text().trim();

Upvotes: 2

Views: 90

Answers (1)

Scimonster
Scimonster

Reputation: 33409

:not() is a CSS3 selector, and as such is available via querySelector[All] in modern browsers.

alert(document.querySelector("#firstDiv :not(b)").innerText.trim());
<div id="firstDiv">
    <b>Item 1:</b> <span>tacos</span>
</div>

I've also wrapped the "tacos" in a <span> to facilate access, and changed the selector to select descendants that aren't <b>s.

Upvotes: 2

Related Questions