-6

I'm trying to reference the id of a div-table row in the variable thisId. When I console.log(thisId), it says that thisId is undefined. What am I doing wrong?

$('.deleteButton').click(function(){
    var thisId = $(this).parent().parent().id;
    for (var i = 0; i < tableData.length; i++) {
        if (tableData[i].rowValue === thisId) {
            tableData.splice(thisId, 1);
        }
    }
    $(this).parent().parent().remove();
});
}
}

HTML

"<div id='" + tableData[i].rowValue + "' class=\"Row\">" +
                            "<div class=\"Cell\">" +
                            "<p>" + tableData[i].textInput + "</p>" +
                        "</div>" +
                        "<div class=\"Cell\">" +
                            "<p>" + tableData[i].perIntervalInput + "</p>" +
                        "</div>" +
                        "<div class=\"Cell\">" +
                            "<p>" + tableData[i].radioInput + "</p>" +
                        "</div>" +
                        "<div class=\"Cell\">" +
                            "<button class=\"deleteButton\">Delete</button>" +
                        "</div>" +
                        "</div>"
Jeremy
  • 149
  • 2
  • 14
  • 3
    Where's the HTML that this would reference? – Marc Nov 12 '15 at 16:02
  • 2
    `.id` is a property on DOM Elements, not jQuery. – Stryner Nov 12 '15 at 16:02
  • 3
    This is a duplicate of [http://stackoverflow.com/questions/3239598/how-can-i-get-the-id-of-an-element-using-jquery](http://stackoverflow.com/questions/3239598/how-can-i-get-the-id-of-an-element-using-jquery) – Castyr Nov 12 '15 at 16:03
  • .attr or .prop didn't seem to help my issue – Jeremy Nov 12 '15 at 16:14
  • Why not just `$(this).closest('[id]').remove();`? Why do you need to 'reference' ID? What is exactly your expected behaviour? – A. Wolff Nov 12 '15 at 16:15
  • I already have the div removal covered (`$(this).parent().parent().remove();` works fine), I'm trying to reference the ID so I can remove the object in my array that corresponds with the div. – Jeremy Nov 12 '15 at 16:17
  • 1
    @germz So `var thisId = $(this).closest('[id]')[0].id;` or `var thisId = $(this).closest('[id]').attr('id');` or whatever but your question is still unclear – A. Wolff Nov 12 '15 at 16:18
  • I need the ID because it is the same as the rowValue property in an array object that corresponds with the row. I need to remove that object when .deletebutton is clicked. I realize that my method for that is wrong, but right now I'm just trying to figure out how to actually get the ID of the parenting row div. – Jeremy Nov 12 '15 at 16:30

2 Answers2

5

ID is an attribute and so can be reference via the attr(..) function.

var thisId = $(this).parent().parent().attr("id");
George Lee
  • 826
  • 6
  • 11
  • attempted .attr as well, same result. – Jeremy Nov 12 '15 at 16:07
  • As others have mentioned, you need to expand on your question. We need to see the HTML that you are working with and how it refers to your JS. – George Lee Nov 12 '15 at 16:09
  • Just added the html (it's messy looking because it's being appended) – Jeremy Nov 12 '15 at 16:12
  • If the above code still doesn't work it seems there must be something else going wrong with your script. As a check, try `console.log($(this).parent().parent().get(0).outerHTML);` and see if it is actually the code you expect it to be. – Doug McLean Nov 12 '15 at 17:45
1

Since you're using jQuery, you can use:

var thisId = $(this).parent().parent().attr('id');

But your HTML isn't here to show us what you're actually referencing, and whether you're referencing it properly, so there might be something else in play.

Marc
  • 11,403
  • 2
  • 35
  • 45