1

I am using the following code to make the text red in any span with the class code and which contains text 'Failed to send invite'. This works however the table is part of pagination results set and the pagination is updated via ajax so when they change the page of results the red color is lost.

How do I make this code work in way the will work with the ajax content.

//This works for setting red in the initial page load   
$('table span.code').each( function() {
    if( /(Failed to send invite)/.test($(this).html())) {
        $(this).css('color', 'red');
    }
});
2
  • 1
    Are you using ASP.NET and UpdatePanels? Commented Jul 23, 2013 at 13:28
  • Add an event to the pagination buttons which executes this code. Commented Jul 23, 2013 at 13:30

2 Answers 2

2

Have you tried including this code when the new content is written to the page? jQuery doesn't work with live updates to the page, it runs once (unless you tell it otherwise).

You could always turn this into a function and just run the function every time you needed to loop through your table.

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

Comments

0

You have to rebind event handlers after ajax request refreshes part of a page:

$().ready(function () {
    bindHandlers();

    function doAjaxCall() {
        $.ajax({
            ...some codes,
            success: function (result) {
                ...some codes
                bindHandlers();
            }
        });
    }

    function bindHandlers() {
        $('table span.code').each( function() {
            if( /(Failed to send invite)/.test($(this).html())) {
                $(this).css('color', 'red');
            }
        });
    }
});

Comments

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.