Ok so I have this drop down and I want to bold the first word in the main level of the nav..See which ones in my code below with comments.
To make it clear I would like the words Our, Lorem, and Another to be wrapped in a strong
tag or a span
tag since they are the first words in the main level of the navigation. How can I accomplish this with jQuery?
<ul class="clearfix" id="topnav">
<li class="mainNavFirst"><a href="/">Our Home</a></li> <!-- bold this 'our' -->
<li class="mainNavMiddle"><a href="#">Lorem Ipsum</a><!-- bold this 'Lorem' -->
<div>
<ul>
<li class="sectionTitle">
<h2><a href="#">Ipsum Lorem</a></h2>
</li>
<li><a href="#">Just a random link</a></li>
<li><a href="#">Just a random link</a></li>
</ul>
<ul>
<li class="sectionTitle">
<h2><a href="#">Ipsum Lorem</a></h2>
</li>
</ul>
</div>
</li>
<li class="mainNavLast"><a href="#">Another Link</a></li><!-- bold this 'Another' -->
</ul>
$('#topnav > li > a').html(function(i,html){
return html.replace(/^\s*([^\s]+)(\s|$)/, '<span>$1 </span>');
});
http://jsfiddle.net/LKXKs/3/
EDIT: Fixed it so it will still work if there's only one word in the <a>
, or if there are leading spaces.
$(function(){
$('#topnav > li > a').each(function(item, index){
var firstWord = $(this).text().split(" ")[0];
var newText = $(this).text().replace(firstWord, "<span>"+firstWord +"</span>");
$(this).html(newText);
});
});
Then use CSS style:
#topnav > li > a span { font-weight: 800; }
Fiddle: http://jsfiddle.net/zbfcS/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With