0

I have the follwing variable:

var workerId = "worker1";

I also have this div:

<div role="tabpanel" class="tab-pane" id="worker1">

Is it possible to update my variable to use the id from the div rather than hard coding the value?

pac
  • 491
  • 1
  • 7
  • 30

1 Answers1

4

If that element class is unique you can get the element by class and read the id from that element:

const el = document.getElementsByClassName('tab-pane');
const workerId = el[0].id;
console.log(workerId);
<div role="tabpanel" class="tab-pane" id="worker1">
a--m
  • 4,716
  • 1
  • 39
  • 59
  • No. Unfortunatelyy I currently have three of that element class. – pac Mar 22 '18 at 07:33
  • 1
    In that case you should have something that identifies that element in a unique way. Could you add some data-* attribute to the element? – a--m Mar 22 '18 at 09:58