gpt-3.5-turbo-0125· openai
Suzhen/CodeChat
How can I read a data-popup value in plain Javascript?
- User
How can I read a data-popup value in plain Javascript?
- Assistant
To read a data-popup value in plain JavaScript, you can use the
getAttribute()method on the element you want to read the data value from.Here's an example:
<!DOCTYPE html> <html> <head> <title>Read data attribute value</title> </head> <body> <div id="element" data-popup="popup-value">This is the element</div> <script> // Get the element by id var element = document.getElementById("element"); // Get the data-popup value using getAttribute method var dataValue = element.getAttribute("data-popup"); // Log the data value to the console console.log(dataValue); </script> </body> </html>In this example, the
elementvariable usesdocument.getElementById()to select the element with the id of "element". It then uses thegetAttribute()method to read the data-popup value and store it in thedataValuevariable. Finally, the data value is logged to the console.