Getting closest element by id

Question :

Getting closest element by id,

Answer :

I have two elements:


When b is clicked, I want to access a and manipulate some of its data. A does not have a globally unique name, so document.getElementsByName is out. Looking into the event object, I thought would have some function like getElementsByName, but this does not seem to be the case with

s. Is there any simple way to do this?


If a and b are next to each other and have the same parent, you can use the prevSibling property of b to find a.

That’s the answer Getting closest element by id, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Read More  What rule engine should I use?

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you