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?

9
  • 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. Commented Dec 26, 2013 at 4:25
  • okey please see this: jsfiddle.net/2GENx/9 this doesn't work either. nor this jsfiddle.net/2GENx/10 Commented Dec 26, 2013 at 4:29
  • It is wrong again your ajax call does not completed and you are trying to replace your data. How it is possible? Commented Dec 26, 2013 at 4:31
  • You should be sure that you are calling your replaceText after ajax call completed. Commented Dec 26, 2013 at 4:33
  • where are you calling your ajax call? Commented Dec 26, 2013 at 4:37

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

7 Comments

@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: jsfiddle.net/2GENx/5
Sorry for putting -1 to your answers. Please update your answer and I will be able to remove it.
@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.
@Jack please update your answer then I can remove -1.
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.