I am trying to find the closest element that has id attribute and get the id. The element could be , or other elements. I
id
, or other elements. I
,
I
You can get rid of the div entirely and look for all elements that have an id attribute:
div
$('#test').closest('[id]').attr('id');