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

jQuery string .replace() method

0 votes
51 views
added Mar 16, 2018 in jQuery by Callum Sergeant (1,480 points)
edited May 4, 2018 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 Responses

0 votes
responded Mar 26, 2018 by LC Marshal Captain (24,040 points)
edited May 4, 2018 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, 2018 by birdygeek Sergeant (2,060 points)
edited May 4, 2018 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, 2018 by LC Marshal Captain (24,040 points)

To remove element from element

$('.boss').html().replace('.worker1', '');
0 votes
responded May 4, 2018 by LC Marshal Captain (24,040 points)

replace()  mutiple characters with regex

replaceName = endName.replace(/[-?1234567890=]/g, ' '),
0 votes
responded May 4, 2018 by LC Marshal Captain (24,040 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, 2018 by LC Marshal Captain (24,040 points)
//redirect page into specific link
window.location.replace("https://lazacode.org");
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...