I need to Show / Hide options of select list based on the value of a first select list but I can't use the value of the second one I need to use data attributes or something else, I try this from a snippet I found that works fine on values but it seems that I misuse the data method
I Show you my Html and js :
$(document).ready(function() {
var optarray = $("#group").children('option').map(function() {
return {
"data": this.data,
"option": "<option data='" + this.data + "'>" + this.text + "</option>"
}
})
$("#type").change(function() {
$("#group").children('option').remove();
var addoptarr = [];
for (i = 0; i < optarray.length; i++) {
if (optarray[i].data.indexOf($(this).data()) > -1) {
addoptarr.push(optarray[i].option);
}
}
$("#group").html(addoptarr.join(''))
}).change();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select name="type" id="type">
<option value="">Choose a Type</option>
<option value="music">Music</option>
<option value="art">Art</option>
<option value="cook">Cook</option>
</select>
<select name="group" id="group">
<option value="">Choose a group</option>
<option value="band" data="music">Band</option>
<option value="guitarplayer" data="music">Guitar Player</option>
<option value="painter" data="art">Painter</option>
<option value="graffer" data="art">Graffeur</option>
<option value="chief" data="cook">Chief</option>
</select>
Thx guys (and girls (:)
jQuery.data(). Usage is wrong in markup as well asthis.dataandthis.textare undefined.thiswill be a DOM element. Using your console to look a errors will help