Search code examples
javascriptattributesgetattribute

Is it possible to capture the attribute of another element with an onclick event handler?


The question's pretty self-explanatory.

Instead of capturing from itself like: this.getAttribute('id')) or this.id; is it possible to capture an attribute from another tag?

EDIT:

Sure, with an onclick:

Let's say:

<button onclick="Submit_Click("here I want to catch the attribute of another tag, not this one")">Save changes</button>

Solution

  • You can use querySelector or getElementbyId like this:

    <button onclick="Submit_Click(document.getElementById('id-of-other-element').getAttribute('id'));">Save changes</button>