user3358102
user3358102

Reputation: 317

Use div elements in Javascript file

I have a div which has different values defined

<div class="scontent" scountry="en-GB" snumber="22000"></div>

In addition I have an external Javascript file which should use the data provided in the div above and output the info.

<script>document.write("22000en-GB"); </script>

Is it possible with plain Javascript to look for the div with class="scontent" and include the values of scountry and snumber into the Javascript code dynamically?

Upvotes: 0

Views: 36

Answers (2)

Avinash A
Avinash A

Reputation: 793

You can get all additional attributes inside dataset of any element

const ele = document.getElementsByClassName("scontent")[0].dataset;
const scountry = ele.scountry;
const snumber = ele.snumber;

Upvotes: 1

Scott
Scott

Reputation: 262

let element = document.getElementsByClassName('scontent')[0];
let scountry = element.getAttribute('scountry');
let snumber = element.getAttribute('snumber');

Upvotes: 2

Related Questions