Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bold First Word In Dropdown with jQuery

Tags:

html

jquery

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>
like image 656
breezy Avatar asked May 17 '11 20:05

breezy


2 Answers

$('#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.

like image 54
user113716 Avatar answered Sep 23 '22 18:09

user113716


    $(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/

like image 20
Mutt Avatar answered Sep 26 '22 18:09

Mutt