I'm trying to use jQuery's contains word selector to hide / show some elements dynamically on my page, however the selector is not working as expected. I'm using the 3.1.1
version of jQuery.
Notice by% w_that the code considers only the last value of the attribute when selected, so in cases B and C there are the impression that the selection worked .. however the A / B and A / C options should be visible also when A is selected again.
Anyone have any idea why? Is it a bug? Or is it really supposed to work? (not what the documentation implies).
Thank you in advance!
var options = {
A: $('*[data-visibleOn~="A"]'),
B: $('*[data-visibleOn~="B"]'),
C: $('*[data-visibleOn~="C"]'),
}
$("#opt").on('change', function(evt) {
var selected = $(this).val();
for(var key in options) {
if(options.hasOwnProperty(key)) {
if(key == selected) {
options[key].show();
} else {
options[key].hide();
}
}
}
})
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script><selectname="option" id="opt">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<div data-visibleOn="A">A</div>
<div data-visibleOn="A B">A ou B</div>
<div data-visibleOn="B">B</div>
<div data-visibleOn="A C">A ou C</div>
<div data-visibleOn="C">C</div>