2

I'm trying to apply CSS to HTML elements that have specific HTML within them using Javascript/jQuery. What I've got is basically:

<ol>
<li>*<a href=http://foo.com/1>A bunch of text</a>
<li><a href=http://foo.com/2>More text</a>
<li>*<a href=http://foo.com/3>Even more text</a>
<li><a href=http://foo.com/4>Just a little more text</a>
</ol>

What I want is to run .addClass() on the two <li> tags that are followed by an asterisk.

I know I could use $("ol li:contains(*)") but there could be asterisks in the link text, so I can't trust that to be 100% accurate. Ideally, I'd like to find a way to run .addClass() on all <li> tags that are followed by *<a href=http://foo.com/, but just a way to run it on <li> followed by * would probably do it.

I looked at the answer to this question, but it doesn't help, even if I switched out innerText for innerHTML, because I'm not looking to match the whole HTML of the <li>, just the beginning.

1
  • 1
    You are missing the closing </li> tags. Commented Nov 10, 2013 at 1:30

1 Answer 1

4
$("ol li").filter(function(){
    return $(this).text().charAt(0) === '*';
}).addClass('awesome');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.