0

I have a span element like this:

<span meta-nextep="ep-2-2" class="player-overlay radius4 show ep-2-1" meta-serie="TEST" meta-stag="2" meta-ep="1" meta-embed="TESTLINK" meta-embed2="TEST2" meta-embed3="TEST3"></span>

And I want to get the attribute of meta-embed, which is "TESTLINK". I've tried document.getElementById(), but it doesn't work, do you know how i can do it?

3
  • .getAttribute(). Commented Oct 10, 2019 at 13:56
  • 1
    It really should be data- and not meta- so you can use dataset and be valid Commented Oct 10, 2019 at 13:58
  • Possible duplicate of Get value of a Custom Attribute using Javascript or Jquery Commented Oct 10, 2019 at 14:05

1 Answer 1

1

There is no id for this span element so you can use querySelectorAll with a class name and use .getAttribute to get the attribute you want from.

const res = [...document.querySelectorAll(".player-overlay")].map(el =>

  el.getAttribute("meta-embed")
)

console.log(res)
<span meta-nextep="ep-2-2" class="player-overlay radius4 show ep-2-1" meta-serie="TEST" meta-stag="2" meta-ep="1" meta-embed="TESTLINK1" meta-embed2="TEST2" meta-embed3="TEST3"></span>

<span meta-nextep="ep-2-2" class="player-overlay radius4 show ep-2-2" meta-serie="TEST" meta-stag="2" meta-ep="1" meta-embed="TESTLINK2" meta-embed2="TEST2" meta-embed3="TEST3"></span>

<span meta-nextep="ep-2-2" class="player-overlay radius4 show ep-2-3" meta-serie="TEST" meta-stag="2" meta-ep="1" meta-embed="TESTLINK3" meta-embed2="TEST2" meta-embed3="TEST3"></span>

Sign up to request clarification or add additional context in comments.

2 Comments

And what if i have class="player-overlay radius4 show ep-2-2 and class="player-overlay radius4 show ep-2-3" etc and i want get the meta attribute of everyone ?
i updated my code for an example of group of span update your question please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.