JordanBarber JordanBarber - 11 months ago 30
HTML Question

Why is JS 'id' returning undefined

Below is my code:

var activeSlide = $('.slide').filter(':visible');
var currentId =;

My first console log is returning the HTML object:

[div#slide-2.slide.current, prevObject: p.fn.init[5], context: document]

But my second console.log() is returning 'undefined'. Am I missing something here?


Your activeSlide variable has a jQuery object assigned to it, and that won't have an "id" property.

You can get the id with .prop():

var currentId = activeSlide.prop("id");

or, if you don't want to make a function call:

var currentId = activeSlide[0].id;

However if you do that you run the risk of an exception if there's really no active slide.