1

I have a method that adds an image to a TD and attaches the click event to the image. I am dynamically building a row and wanted to attach the TD image column to my row.

$("<tr></tr>").append(addActionColumn($("<td></td>")));

And here is my addActionColumn method:

function addActionColumn(td) {



    var actionImage = document.createElement("img");
    $(actionImage).attr("src", "../Images/cross_red.JPG");

    // delete the row
    $(actionImage).click(function() {

        $(td).parent().remove();

     });


    $(td).append(actionImage);
}

For some reason the TR never gets the image column attached to it.

3 Answers 3

2

Your function doesn't return the td element you create.

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

1 Comment

Thanks! I am having a case of the Fridays!
1

I'd do something like this:

var td = $('<td />');
$('<img />').addClass('close-button').prop('src', '../Images/cross_red.JPG').appendTo(td);

$('<tr />').append(td).appendTo('#your_parent_element');

$('.close-button').on('click', function() {
  $(this).parent().remove();
});

Comments

1

I think it is because you are trying to .append the result of the addActionColumn which is null because you are not returning a value in the function.

try something like this at the end of the function:

return $(td);

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.