Jaumenik
Jaumenik

Reputation: 43

How to read specific value from button in js function

I have several buttons contain some values:

<button class="Hotspot" onclick="ChangeView()" slot="hotspot-1" camera.Orbit='2.895deg 85deg 27.92m' ;>

and I need to create function that read camera.Orbit value from button's container after click on it:

function ChangeView() {
    const modelViewer = document.querySelector('#orbit-demo');
    modelViewer.cameraOrbit = camera.Orbit ???
}

I have no idea how to solve this. I do not use getElementbyId because it has to work for all buttons (not specific one). I barely can JS.

Thank you for help.

Upvotes: 1

Views: 41

Answers (2)

TheElmo
TheElmo

Reputation: 86

The onclick function passes the click event to the method. You can take the target of this event (the button element) and acquire tha attribute like that.

onclick(e => changeView(e))
ChangeView(e) {
    // do things with e.target.???
}

Upvotes: 0

Spectric
Spectric

Reputation: 31992

You can use Element.getAttribute:

function ChangeView() {
  const modelViewer = document.querySelector('#orbit-demo');
  let orbit = modelViewer.getAttribute('camera.Orbit')
  console.log(orbit)
}
<button class="Hotspot" onclick="ChangeView()" slot="hotspot-1" camera.Orbit='2.895deg 85deg 27.92m' id="orbit-demo">Click</button>

Upvotes: 2

Related Questions