2

I need to replace (with jquery) the text for an element which is not loaded on document.ready. The element is loaded with ajax.

I tried with:

    $('#my-div label').live({
        var text = $(this).text();
        $(this).text(text.replace('text', 'my text')); 

    });

the html is something like

<div id="my-div"><label>text</label></div>
4
  • 1
    You have to do it on your success ajax handler. Please paste it on your question Commented Jun 28, 2013 at 15:09
  • 1
    Since it's loaded via AJAX, in your AJAX callback function, you have the element. Can't you change the element in that moment? Commented Jun 28, 2013 at 15:09
  • I can't modify the AJAX function. Is a drupal module, I will lost my changes every time I update the module... Commented Jun 28, 2013 at 15:13
  • 1
    user $(document).ajaxComplete Commented Jun 28, 2013 at 15:14

3 Answers 3

6

Try this...

$(document).ajaxComplete(function() {
    var $label = $("#my-div label");
    var text = $label.text();
    $label.text(text.replace("text", "my text")); 
});

That will fire that function every time an ajax call is completed. That's ANY ajax call on that page.

Sign up to request clarification or add additional context in comments.

Comments

2

You don't need to use live. You aren't binding any event here. In the success function of your ajax, just put

    var label = $('#my-div label');
    label.text(label.text().replace('text', 'my text')); 

Comments

2
$(document).ajaxComplete(function() {
  $('#my-div label').text('my text');
});

try to use ajaxComplete or ajaxSuccess

1 Comment

works even without ajaxComplete, ajaxSuccess, etc.

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.