2
I’m trying to use the "contains word selector" from jQuery to hide/show some elements dynamically on my page, however the selector is not working as expected. I am using the version 3.1.1
jQuery.
Note by snippet
that the code considers only the last value of the attribute when selected, therefore in cases B and C there is the impression that the selection worked.. however the options A/B and A/C should also be visible when the option To is selected again..
Does anyone have any idea why this is? Is it a bug? Or is it how it should really work? (That’s not what documentation).
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>
<select name="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>
Aaah that beast... is obvious, was showing and then hid the other dials. kkkk Thanks for the answer face.. I was blind in that ñ had called me.
– Israel Merljak
You just have to take care of that dial
*=
is different from~=
.– fernandosavio