0

how to fetch value of data-summary attribute of span using java script

example

<span id="summary-text" data-summary="this value needed">summary of contents  </span>

example this value needed

1

2 Answers 2

2

Use the dataset object to retrieve attributes prefixed with 'data'.

var data = document.getElementById("summary-text").dataset.summary;
console.log(data); //this value needed

MDN API

JSBIN

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

3 Comments

That's the way with ES6 :-)
@DaveEveritt I don’t follow.
.dataset is supported in the relatively recent ES6 version of JavaScript. The older way is .getAttribute
-1

document.getElementById('summary-text').getAttribute('data-summary') should work.

Comments

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.