0

I am trying to find and replace texts using jquery.

function replaceText() {
    var jthis = $(this);
    $("*").each(function() { 
        if(jthis.children().length==0) { 
            jthis.text(jthis.text().replace('nts:', 'nights:')); 
        } 
    });
}
$(document).ready(replaceText);
$("html").ajaxStop(replaceText);

here is my jsfiddle:

http://jsfiddle.net/2GENx

I need to replace the all "nts" texts on the page by "nights". Can you tell me why it's not working?

Johnny
  • 1,685
  • 6
  • 24
  • 42
  • Your problem is `$("html").ajaxStop(replaceText);` never triggered. Just put break point into beginning of replaceText function. And count how many times it called. It should be called two times. First when document is ready. Second when Ajax load completes. Another problem as others explained you should also modify replaceText function logic as one explained bellow. – Khamidulla Dec 26 '13 at 04:25
  • okey please see this: http://jsfiddle.net/2GENx/9/ this doesn't work either. nor this http://jsfiddle.net/2GENx/10/ – Johnny Dec 26 '13 at 04:29
  • It is wrong again your ajax call does not completed and you are trying to replace your data. How it is possible? – Khamidulla Dec 26 '13 at 04:31
  • You should be sure that you are calling your replaceText after ajax call completed. – Khamidulla Dec 26 '13 at 04:33
  • where are you calling your ajax call? – Khamidulla Dec 26 '13 at 04:37
  • I am bit lost. Tryin to work this out since 2 hours. Can you please update the jsfiddle? – Johnny Dec 26 '13 at 04:39
  • No I cannot because I cannot find out where you are calling ajax request. It should be in your external files. However your external files all minimized. It is hard to analyze your minimized code. – Khamidulla Dec 26 '13 at 04:41
  • yes it is in external file. I am just trying to include this external page to my page by changing the texts. – Johnny Dec 26 '13 at 04:49
  • I am including it using `echo file_get_contents($url);` just wanted to show you the source code on jsfiddle. So in this case it is impossible to change the text because its unknown when request is finishes? – Johnny Dec 26 '13 at 04:58

1 Answers1

1

I see that you have tried to avoid writing $(this) all the time by storing its value in jthis; the problem is that by doing so you effectively always inspect the same item.

Instead, save the reference inside the each() callback:

function replaceText() 
{
    jQuery("*").each(function() {
        var $this = jQuery(this);
        if ($this.children().length==0) { 
            $this.text($this.text().replace('nts:', 'nights:')); 
        } 
    });
}

It also seems that you're using jQuery next to something called wisdomweb on your page, and that doesn't support the .ajaxStop() feature; the only suggestions I can give you:

  1. hack the library to support .ajaxStop(),
  2. listen for dom changes and perform the replacement inside,
  3. use an even uglier setTimeout() option.
Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • @antindexer what you mean by "jsFiddly under console" I don't have any error on my chrome console. please can you explain If you have noticed where the problem is. Jack This didn't work either. please see: http://jsfiddle.net/2GENx/5/ – Johnny Dec 26 '13 at 04:23
  • Sorry for putting -1 to your answers. Please update your answer and I will be able to remove it. – Khamidulla Dec 26 '13 at 04:32
  • 2
    @hijacker83 The problem is that you're using jQuery but `$w` is not jQuery at all, it's from wisdomweb. Currently it seems that they have no way to know when an AJAX request has finished. – Ja͢ck Dec 26 '13 at 04:41
  • @Jack please update your answer then I can remove -1. – Khamidulla Dec 26 '13 at 04:43
  • @Jack I am including it using `echo file_get_contents($url);` just wanted to show you the source code on jsfiddle. So in this case it is impossible to change the text because its unknown when request is finishes? – Johnny Dec 26 '13 at 04:57
  • @hijacker83 If you're doing that with PHP, why not do the replacement there as well then? – Ja͢ck Dec 26 '13 at 04:58
  • @Jack because of "nts" doesn't exist on page load (please click to "show source code" on your browser you will see that actualy "nts" texts doesn't exists it is requested later) and I thought the only way is to change it by using jquery after page is loaded. – Johnny Dec 26 '13 at 05:07