I am trying to find the closest element that has id attribute and get the id
. The element could be <p>, <div>
or other elements.
I have tried
element=$('#test').closest('div[id]').attr('id');
but it only get the id if the element is a div
. I want to be more universal. Are there anyways to do this? Thanks so much!
You can get rid of the
div
entirely and look for all elements that have anid
attribute: