1

DEMO

Hi Am trying to remove the arrows from JS. but seems like replace is not working :-(

<a class="pr-page-prev">« Previous</a>
<a class="pr-page-next">Next »</a>


$('.pr-page-prev').text().replace(/&laquo;/, '')
$('.pr-page-next').text().replace(/&raquo;/, '')
Developer
  • 1,409
  • 2
  • 23
  • 46
  • 3
    There is no `«` in the text you are getting it's the actual symbol: `«`, have you tried replacing that? Also you are doing nothing with the result: http://jsfiddle.net/peteng/t1rru99a/3/ – Pete Mar 17 '15 at 09:35
  • possible duplicate of [jquery text().replace('','') not working](http://stackoverflow.com/questions/28025944/jquery-text-replace-not-working) and many others – JJJ Mar 17 '15 at 09:40

5 Answers5

9

You are replacing the text but not the content of the element.

When you say $('.pr-page-prev').text(), it returns the text content on which you are calling replace which will return the replaced text but it is not set back to the element. So even if you have replaced the text it won't be applied to the element.

$('.pr-page-prev').text(function(i, text){return text.replace(/«/, '')})
$('.pr-page-next').text(function(i, text){return text.replace(/»/, '')})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
3

That's because you are not doing anything with the results of the replace:

e.g.

$('.pr-page-prev').text($('.pr-page-prev').text().replace(/«/, ''));

Or using a function instead of the value:

$('.pr-page-prev').text(function(){ return $(this).text().replace(/«/, ''));

http://jsfiddle.net/TrueBlueAussie/t1rru99a/6/

Or better yet, combine them both into a single operation (the Regex shown will match either character):

$('.pr-page-prev,.pr-page-next').text(function () {
    return $(this).text().replace(/[«»]/, '')
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/t1rru99a/9/

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
2

You can replace the value "«" with "space".

$(".pr-page-prev").text(function(i, val) {
    return val.replace(/«/g, " ");
});
$(".pr-page-next").text(function(i, val) {
    return val.replace(/»/g, " ");
});

Working Demo

chiapa
  • 4,362
  • 11
  • 66
  • 106
stanze
  • 2,456
  • 10
  • 13
1

You're replacing the string as a result but not changing what is displayed... what you need to do is to use the text() method to get and to set the data, here's an example :

$('.pr-page-prev').text($('.pr-page-prev').text().replace("«", ''))
Khalid
  • 4,730
  • 5
  • 27
  • 50
1

The problem is that you are trying to access/read the text but not write it back. To write it back you need to replace the text as well. You can do it by JSFiddle.

$('.pr-page-prev').text($('.pr-page-prev').text().replace("«", ''))
$('.pr-page-next').text($('.pr-page-next').text().replace("»", ''))
Abhishek
  • 6,912
  • 14
  • 59
  • 85