Marie J Marie J - 1 month ago 6
jQuery Question

How can I get just the first word from an address link text with jQuery?

I have the following:

<a title="Home" href="#">Home test test</a>


I can use a.html() to get me "Home test test" but what I need is just the first word "Home". Is there an easy way that I can do this with jQuery?

Answer Source

demo http://jsfiddle.net/HvFMp/

split: (further read if you keen) http://www.w3schools.com/jsref/jsref_split.asp

another demo using match http://jsfiddle.net/LAKqB/ (using match)

code

$.fn.ready(function(){

  alert($('a').html().split(' ')[0]);    

});
​

code using match api

$.fn.ready(function(){
  var str = $("a").text().match(/\w+/);

  alert(str);     

});
​