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

jQuery string .replace() method

0 votes
20 views
added Mar 16 in jQuery by Callum Corporal (1,350 points)
edited May 4 by LC Marshal

Find body element that has this text and .replace() it with something else

$('body').each(function() {
  var text = $(this).text();
  $(this).html($(this).html().replace('.00', '').replace('.0', '')); 
});

 

6 examples

0 votes
responded Mar 26 by LC Marshal First Warrant Officer (8,170 points)
edited May 4 by LC Marshal

.replace() text inside the element

$(".text-wrapper").text(function () {
    return $(this).text().replace("old text", "new replaced text"); 
});​​​​​
0 votes
responded Mar 26 by birdygeek Sergeant (2,020 points)
edited May 4 by LC Marshal

.replace() multiple sentence or word from multiple elements

$('.listing-type, .listing-info-detail').text( function() {
      return $(this).text().replace("old word", "another old word").replace("new word", "another new word"); 
});
0 votes
responded May 4 by LC Marshal First Warrant Officer (8,170 points)

To remove element from element

$('.boss').html().replace('.worker1', '');
0 votes
responded May 4 by LC Marshal First Warrant Officer (8,170 points)

replace()  mutiple characters with regex

replaceName = endName.replace(/[-?1234567890=]/g, ' '),
0 votes
responded May 4 by LC Marshal First Warrant Officer (8,170 points)

Change text of selector

var catLabel = $('body.page-node-add-forum .form-item-taxonomy-forums-und label');

$(catLabel).text( function() {
    return $(this).text().replace("Forums ", "Choose question category"); 
});
0 votes
responded May 4 by LC Marshal First Warrant Officer (8,170 points)
//redirect page into specific link
window.location.replace("https://lazacode.org");
lazacode.org - Malaysia based Knowledge Exchange Platform, where you can share your finding or solution with other members of community.
...