-2

How do I get the data attributes "name" and "description" in javascript? (user clicks on button, display value)

<form action="http://serverhost/postdata" method="POST">
  <script src="myscript.js" class="blue-button" 
  data-name="aaa" 
  data-description="bbb"
  </script>
< /form>
5

2 Answers 2

4

Like this:

document.querySelector('script.blue-button').dataset

Here is the reference:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset

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

Comments

1

First you should close the opening script tag (makes the syntax high lighting work and is easier to read)

<form action="http://serverhost/postdata" method="POST">
  <script src="myscript.js" class="blue-button" 
  data-name="aaa" 
  data-description="bbb">
  </script>
</form>

Then all you would have to do is

document.querySelector('script.blue-button').dataset

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.