3

I'm trying to replace the contents of a td tag in a javascript function but can't figure out what's going on. Here is the function.

function actionCompleted(response, status, data) {

    // Set the id for row
    var rowId = "#rowId-" + response.fieldname;

    // Set the ids for the two table cells we need
    var tdstatusId = "#tdstatusId-" + response.id;
    var tdreasonId = "#tdreasonId-" + response.id;
    alert(tdreasonId);

    try {
        //get the table cell for the status
        var tdstatus = $('#itemDetails').find(rowId).find(tdstatusId);
        //get the table cell for the reason
        var tdreason = $('#itemDetails').find(rowId).find(tdreasonId);

        //Make sure we found our cells
        if (tdstatus != null && tdreason != null) {
            //Set our cell content
            //tdstatus.html(response.ChangeStatus);
            //tdreason.text(response.message); 
            tdstatus.html('TEST');
            tdreason.text('TEST'); 
        }
    }
    catch (e) {
        alert(e.toString());
    }
}

I first thought there might be a problem with jquery finding the td controls, so I added the null check.

Then I thought it was the .text() so I tried .html().

I thought maybe it was swallowing an exception so I added the try..catch.

Then I thought it might be an issue with the response object the function received, so I threw some alerts in there and they have the values I need.

I checked the Ids and made sure they matched the id's found in the html of the page.

I've checked everything I could think of. But my code simply doesn't work. The first one of the two TD elements I'm trying to set contains two links (a tags), but the second one is empty. So I don't think that has anything to do with it. I'm at wits end here trying to figure this out.

Is there something I'm doing wrong? Could there be something else that would cause this behavior?

Thanks in advance.

2
  • Could you add (the relevant part of) the HTML? Commented Aug 3, 2011 at 19:18
  • 1
    So the var tdstatus and tdreason are working? Commented Aug 3, 2011 at 19:28

2 Answers 2

4

Since jQuery calls always return a jQuery object they will never be null. To test if a wrapped set is empty or not you can check the length.

ie. if (tdstatus.length > 0 && tdreason.length > 0)

Also, I'm not sure what is invoking this function, but if it's being used as an AJAX callback, the signature for it is actually f(data, textStatus, jqXHR).

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

3 Comments

That helped. My table id was incorrect. I had assumed because my cell objects weren't null and that the find on the table row didn't throw an exception, the row was OK. Turns out it wasn't.
Good to hear. By the way, it would be a lot more efficient if you selected a row like this: $('#tdreasonId-' + response.id, '#itemDetails') instead of selecting 3 elements.
I wasn't aware of that syntax. But doesn't that skip the row find? The second argument in $('#tdreasonId-' + response.id, '#itemDetails') is the table's ID. Is the row ID not needed?
1

I got something sort of similar to your situation working in a jsfiddle:

http://jsfiddle.net/jA4ZQ/1/

Perhaps you can modify that using your code and get it to fail. We can then start from there. Try to keep it as simple and to the point of the problem as possible, it'll make it easier.

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.