Skip to content

Commit 36631f4

Browse files
committed
feat: 🎸 added data attribute example
1 parent 8cf6f4f commit 36631f4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

2024 Prep/machine_coding/vanilla-js/form-validation/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ <h1>Form Validation example</h1>
4141
<button type="submit" class="submit-btn">Submit</button>
4242
</form>
4343

44+
<main>
45+
<h2>Data attributes</h2>
46+
<ul>
47+
<li data-animal-type="bird" onclick="showDetails(this)">Owl</li>
48+
<li data-animal-type="insect" onclick="showDetails(this)">Ant</li>
49+
<li data-animal-type="fish" onclick="showDetails(this)">Gold Fish</li>
50+
</ul>
51+
</main>
4452
<script src="./script.js"></script>
4553
</body>
4654
</html>

2024 Prep/machine_coding/vanilla-js/form-validation/script.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ const onFormSubmit = event => {
1818
}
1919

2020
myForm.addEventListener('submit', onFormSubmit)
21+
22+
function showDetails(animalType) {
23+
const animalTypeVal = animalType.getAttribute('data-animal-type')
24+
const animal = animalType.innerHTML
25+
console.log('🚀 ~ showDetails ~ animalType:', animalTypeVal, animal)
26+
alert(`${animal} is a ${animalTypeVal}`)
27+
}

0 commit comments

Comments
 (0)