tags:

views:

92

answers:

2

Hello

I have a unordered list that contains

<ul id="strip">
     <li><a href="#"><span>This-is a test string</span></a></li>
    <li><a href="#"><span>This is without</span></a></li>
     <li><a href="#"><span>New-test</span></a></li>
</ul>

I need to bold the text before the "-", so in the first <li> "This" is bolded.

I'm stuck in the loop where I should find the "-".

NB: Regular JavaScript, no JQuery :-)

+2  A: 

What's wrong with using the 'b' tag?

Noon Silk
The menu is generated dynamic from a CMS - I have no access to edit the HTML it spits out.
meep
But you can edit the content? Doing this in javascript is going to be a little odd.
Noon Silk
Meep, this really should be done without having to resort to JavaScript.
J-P
I absolutely agree. But it was not possible this time - so I had to fool me around with a JavaScript solution.
meep
+2  A: 

Use stringObject.replace(findstring,newstring) method.
In your case liString.replace(/\b(.*?-.*?)\b/,"<strong>$1</strong>")
full solution:

var lists = document.getElementsByTagName("li");
var listsL = lists.length;

for (var i = 0; i < listsL; ++i){
   liString = lists[i].innerHTML;
   liString = liString.replace(/\b([^-]*-[^\b]*?)\b/,"<strong>$1</strong>");
   lists[i].innerHTML = liString;
}
Eldar Djafarov