You need to access the dataset
property:
document.getElementById("the-span").addEventListener("click", function() { var json = JSON.stringify({ id: parseInt(this.dataset.typeid), subject: this.dataset.type, points: parseInt(this.dataset.points), user: "Luïs" }); });
Result:
// json would equal: { "id": 123, "subject": "topic", "points": -1, "user": "Luïs" }
Related Posts:
- Edit webpage with javascript trick – how to “unedit”?
- Automatically pausing html5 video as the ‘next’ button is pressed (javascript/ jquery)
- Is it possible to write data to file using only JavaScript?
- Remove CSS class from element with JavaScript (no jQuery) [duplicate]
- Is it possible to write data to file using only JavaScript?
- How to make JavaScript execute after page load?
- Set cookie and get cookie with JavaScript
- How can I get the data-id attribute?
- Which is better: or
- Onclick javascript to make browser go back to previous page?
- How to make JavaScript execute after page load?
- Why is my Javascript not working?
- Javascript: getElementById vs getElementsById (both works on different pages)
- mailto using javascript
- mailto using javascript
- Mailto using Javascript?
- How to change css property using javascript
- javascript close current window
- I am trying to make a simple toggle button in javascript
- How to call a JavaScript function within an HTML body
- How to save user input into a variable in HTML and JavaScript
- Hiding a button in Javascript
- Show/hide ‘div’ using JavaScript
- How to make JavaScript execute after page load?
- Get HTML code using JavaScript with a URL
- Calculate percentage Javascript
- How to display image with JavaScript?
- Resize image with javascript canvas (smoothly)
- JavaScript function onhover in
- Check if an element contains a class in JavaScript?
- Hide element by class in pure Javascript
- How to play a mp3 using Javascript? [duplicate]
- mailto using javascript
- Function not defined javascript
- How to submit a form with JavaScript by clicking a link?
- How to call a REST web service API from JavaScript?
- Check a radio button with javascript
- AppendChild() is not a function javascript
- How can I change an element’s class with JavaScript?
- JavaScript: Get image dimensions
- Why write script type=”text/javascript” when the mime type is set by the server?
- Difference between href and data-href in anchor tag in html
- How to select an input element by value using javascript?
- javascript window.location in new tab
- How to create a hidden in JavaScript?
- How do I programmatically set the value of a select box element using JavaScript?
- jQuery find element by data attribute value
- How can I capture the right-click event in JavaScript?
- How do I clear the content of a div using JavaScript?
- What’s the difference between HTML ‘hidden’ and ‘aria-hidden’ attributes?
- Add swipe left/right to web page, but use default swipe up/down
- Check if checkbox is checked with jQuery
- Failed to load resource: net::ERR_FILE_NOT_FOUND loading json.js
- Get class name using jQuery
- How to clear the canvas for redrawing
- Custom Tumblr theme wont save because of non-https urls?
- Double click and run the Php script on mac
- CSS no text wrap
- JavaScript runtime error: ‘$’ is undefined
- character counter – backspace doesn’t reflect on characters remaining
- Open URL in same window and in same tab
- Maintain the aspect ratio of a div with CSS
- Bootstrap dropdown not working
- Vertically centering a div inside another div
- How to move an element after another element using JS or jquery?
- How to move image with css/html
- How to use Javascript to read local text file and read line by line?
- Why is the jquery script not working?
- How to style the option of an html “select” element?
- HTML: How to center align a form
- How to mark-up phone numbers?
- Space between two rows in a table?
- How to add onload event to a div element
- What is the difference between
and ?- Make a div fill the height of the remaining screen space
- html5 error : Element head is missing a required instance of child element title
- Print the contents of a DIV
- How to use PHP inside css file
- jQuery checkbox event handling
- Setting a max character length in CSS
- How to use the “required” attribute with a “radio” input field
- Local Storage vs Cookies
- Redirect website after specified amount of time
- Is there a float input type in HTML5?
- Is there a vr (vertical rule) in html?
- Fire oninput event with jQuery
- WordPress Bootstrap Handburger Menu Wont Open
- Accessing WordPress data via JSON gives HTML data for certain keys
- How to prevent pages from automatically adding line breaks?
- How to add a button that enables the user to insert a link in a textarea located in the front-end?
- Phantom strong tags in wordpress
- Https and Http for iFrame
- html form: redirect page after form completes submit function and posts data
- Putting custom html/js page into Elementor as it’s own block
- Hide Tab Titles from Tabs Block
- WordPress Slider is not working
- how add class in containers
- How would I add this code to a single WordPress page?
- how i can change wordpress post views?
- Can I upload my HTML/CSS/Javascript game onto a WordPress page?