Reputation: 23
I am using the following 3 lines of code in my web app.
selected_build = event.currentTarget.parentElement.parentElement.id;
var jselect = $( "#" + selected_build + " .roomc" ).children(".room");
selected_room = jselect[0].id;
This works in most browsers but in IE 8 I get the error
'0.id' is null or not an object
which points to the third line of code. I assume this has something to do with parentElement only working on Elements, but I can't seem to figure it out.
Upvotes: 0
Views: 717
Reputation: 1075785
I assume this has something to do with parentElement only working on Elements...
If IE8 doesn't have parentElement
, since by definition you're dealing with an element as of event.currentTarget
, you can safely use parentNode
instead.
But as you're using jQuery anyway, why not...use jQuery?
selected_build = $(event.currentTarget).parent().parent().attr("id");
if (selected_build) {
var jselect = $("#" + selected_build + " .roomc").children(".room");
selected_room = jselect.attr("id");
if (selected_room) {
// ... use it
}
}
Note that I'm using .attr("id")
to get the id
. That's because if the jQuery set is empty, .attr("id")
gives you undefined
, whereas [0].id
gives you an error.
And actually — you don't need to use id
s at all here other than at the end for selected_room
:
selected_room =
$(event.currentTarget)
.parent().parent()
.find(".roomc > .room")
.attr("id");
if (selected_room) {
// ... use it
}
...and I bet there's a way to hook up the event so we can avoid that .parent().parent()
, too, which is fragile. But without seeing the structure, it's impossible to suggest something.
Upvotes: 1