3

I sometimes build quick and dirty html menus or other types of lists using a pipe | to separate elements, ie:

Home | edit | etc...

I like to make the pipes bold and colored, and wonder if there is a quick way to select a specific character in css styles. (or maybe JS/jquery)

I do know that I can do this with spans around the pipe, but thought it might be elegant to be able to select just this character.

1 Answer 1

7

You can pass a callback function to the .html() method to wrap those characters in spans for you:

$('p.menu').html(function(i,el) {
    return el.replace(/\|/g, '<span class="sp">|</span>');
});​

http://jsfiddle.net/mblase75/ymE6y/

Sign up to request clarification or add additional context in comments.

4 Comments

that is awesome...jaw drop here.
You're welcome -- please don't forget to accept your favorite answers to your questions.
I'm fairly new to jquery, what are the i and el arguments doing?
@user1803405 It's in the documentation -- i is the (zero-based) index number of the element that's being acted on, while el is the original string (and which I should have named str instead, but too late now).

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.