tags:

views:

42

answers:

3

I'm building a wordpress theme for a client who wants to pull their favorite songs from Hype Machine into the site. I'm doing by grabbing the XML feed.

In terms of styling the results though, I'd like to be able to style the Artist Name differently than the Song Title. Unfortunately, the feed includes both the Artist Name and Song Title in the same anchor elements as such:

<li>
    <a href="link">Artist Name - Song Title</a>
</li>

<li>
    <a href="link">Artist Name - Song Title</a>
</li>

I was wondering if there's a way I can insert span tags (with javascript or Jquery maybe) into the generated html so that the results are like:

<li>
    <a href="link"><span class="artist">Artist Name - </span>Song Title</a>
</li>

<li>
    <a href="link"><span class="artist">Artist Name - </span>Song Title</a>
</li>

My thought was that I could use a javascript to insert the open tag in front of the string then when it finds "- " insert the closing tag. I just don't know JS well enough to know how to do it and google hasn't been able to help much.

Thoughts?

Thanks!

+2  A: 

If you wanted to just do it the simplest possible way -

var artistAndTitle = // wherever you get your info from, xml node or whatever
var infoArray = artistAndTitle.split(' - ');
var artistName = infoArray[0];
var trackTitle = infoArray[1];
var html = '<span>' + artistName + '</span> - <span>' + trackTitle + '</span>';
matt lohkamp
PS - this really is the lazy-ish way of doing it, chances are there's a far cooler one-line solution in jQuery.
matt lohkamp
Thanks Matt. I've played around with your suggestions and otherwise jumped down a number of rabbit holes. Sadly I'm no closer. I can get your code to do what I want when artistAndTitle = specific hard-coded string. What I can't get working is when artistAndTitle = the actual xml file. Still playing...
Jiert
Interesting. what are you setting artistAndTitle to? I'm guessing what you want it to be is an xml node value, which should be a String... and that's not working? Let's say you do something as basic as alert(yourVariableHere) - does it pop up a box with the "artist - title" text in it?
matt lohkamp
A: 

I post this link all the time: http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html

If the source code doesn't do exactly what you want, look at what his code does and maybe you can adapt it. It's pretty fast, in my experience, even when applying changes to a fairly large amount of HTML text.

Pointy
A: 

artistAndTitle = "< span >" + artistAndTitle;

artistAndTitle = artistAndTitle.replace(" - ", " - < / span >");

Amit Behere