Skip to content

Commit ad762cc

Browse files
committed
Adding data-* demo for select options.
1 parent 48766e4 commit ad762cc

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ with.
1010

1111
## My JavaScript Demos - I Love JavaScript!
1212

13+
* [Storing Metadata On Select Option Elements](https://bennadel.github.io/JavaScript-Demos/demos/select-option-dataset)
1314
* [Exploring Prev/Next Mechanics In HTMX](https://bennadel.github.io/JavaScript-Demos/demos/htmx-prev-next)
1415
* [Pixel Art With Alpine.js](https://bennadel.github.io/JavaScript-Demos/demos/pixel-art-alpine)
1516
* [Movie Ranking With Sortable.js And Kendall Tau Distance](https://bennadel.github.io/JavaScript-Demos/demos/movie-rank)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>
6+
Storing Metadata On Select Option Elements
7+
</title>
8+
<link rel="stylesheet" type="text/css" href="./main.css" />
9+
</head>
10+
<body>
11+
12+
<h1>
13+
Storing Metadata On Select Option Elements
14+
</h1>
15+
16+
<select name="friendID">
17+
<option value="">
18+
- Select -
19+
</option>
20+
<option value="1" data-bff="true" data-disposition="spicy">
21+
Naomi
22+
</option>
23+
<option value="2" data-bff="false" data-disposition="calm">
24+
Jon
25+
</option>
26+
<option value="3" data-bff="false" data-disposition="unpredictable">
27+
Kim
28+
</option>
29+
<option value="4" data-bff="false" data-disposition="fun">
30+
Lara
31+
</option>
32+
</select>
33+
34+
<script type="text/javascript">
35+
36+
var select = document.querySelector( "select" );
37+
38+
// Listen for changes in selection.
39+
select.oninput = ( event ) => {
40+
41+
// Ignore re-selection of empty option.
42+
if ( ! select.value ) {
43+
44+
return;
45+
46+
}
47+
48+
var option = select.selectedOptions[ 0 ];
49+
50+
console.group( "Friend Selection" );
51+
console.log( `ID: ${ select.value }` );
52+
console.log( `Name: ${ option.label }` );
53+
// In addition to the core VALUE/LABEL properties, we can consume the OPTION
54+
// just like any other HTML element, which means we can use the `data-*`
55+
// attributes to relay additional information associated with the option.
56+
console.log( `BFF: ${ option.dataset.bff }` );
57+
console.log( `Disposition: ${ option.dataset.disposition }` );
58+
console.groupEnd();
59+
60+
};
61+
62+
</script>
63+
64+
</body>
65+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
:where( html ) {
3+
box-sizing: border-box ;
4+
5+
& *,
6+
& *:before,
7+
& *:after {
8+
box-sizing: inherit ;
9+
}
10+
}
11+
12+
:where( * ) {
13+
&:focus,
14+
&:focus-within {
15+
outline-color: hotpink ;
16+
outline-offset: 4px ;
17+
outline-width: 2px ;
18+
}
19+
}
20+
21+
body {
22+
font-family: Avenir, Montserrat, Corbel, URW Gothic, source-sans-pro, sans-serif ;
23+
font-size: 18px ;
24+
line-height: 1.4 ;
25+
}
26+
27+
button,
28+
input:where([type="text"]),
29+
select,
30+
textarea {
31+
color: inherit ;
32+
font-family: inherit ;
33+
font-size: 20px ;
34+
line-height: inherit ;
35+
padding: 5px 10px ;
36+
}

0 commit comments

Comments
 (0)