Are you first timer at Why not join us and start exchange your knowledge with everyone. Register today!

jQuery .substr() method

0 votes
added Mar 30 in jQuery by theoh_lee Lance Corporal (790 points)
edited Apr 2 by LC Marshal

Get substring before specific character. In this example, we want to get '1345 albany street' text only from div.address-div before the comma 

<div class="address-div">1345 albany street, Bellevue WA 42344</div>


var addy = $('.address-div').text();
var streetaddress= addy.substr(0, addy.indexOf(',')); 

3 examples

0 votes
responded Apr 26 by LC Marshal First Warrant Officer (8,170 points)

First word selector in element

$('div.message').each(function() {
   var html = $(this).html();
   var word = html.substr(0, html.indexOf(" "));
   var rest = html.substr(html.indexOf(" "));
0 votes
responded May 2 by LC Marshal First Warrant Officer (8,170 points)

Change URL by getting last part of URL and combined a few variables to get final URL

var catName = $('.forum-category > a'), 
    catUrl = $(location).attr('href'),
    urlParts = catUrl.split("/"),
    endUrl = urlParts[urlParts.length-2],
    forumPage = '/property-forum/',
    resultUrl = forumPage + endUrl;
    // alert(endUrl);

$(catName).attr('href', resultUrl);
0 votes
responded May 2 by LC Marshal First Warrant Officer (8,170 points)

Get last-child of breadcrumb and use .trim to manipulate URL

var areaname = $('').text(),
    lookforstate = $('ol.breadcrumb li:last-child').text(),
    trimstate = lookforstate.split(","),
    resultstate  = trimstate [trimstate.length-1],
    getstate = $.trim(resultstate),
    w = "/map-search-property?&state=",
    x = "&district="; 
    result = w + getstate + x + areaname;

$('a#search-listing-area').attr('href', result); - Malaysia based Knowledge Exchange Platform, where you can share your finding or solution with other members of community.