lundi 7 janvier 2019

Using custom attributes in javascript

So I'm trying to access custom attributes and use them within javascript if statements or switch statements but I'm running into issues. The changed attribute isn't recognized in the code and the code just runs the first instance of it. I've seen some solutions be using (===) or global variables but I haven't been able to make it work myself. Please help.

<body>
<select id='select' onchange = 'myFunction()'>
            <option data-set = 'Numbers'>One</option>
            <option data-set = 'Numbers'>Two</option>
            <option data-set = 'Letters'>A</option>
            <option data-set = 'Letters'>B</option>
            </select>

<p id='p1'>Example</p>
<script>

function myFunction() {
     var select = document.getElementById('select');
     var selectSet = select.options[select.selectedIndex].dataset.set;
     document.getElementById('p1').innerHTML = selectSet;
}
</script>
</body>

Aucun commentaire:

Enregistrer un commentaire