I need one of these divs to be hidden and one to be visible depending on a value from a drop-down selector.
<div id="selections" class="row mapSlider">
//a slider and some buttons
</div>
<div id="selections" class="row areaSlider">
//a slider and some buttons
</div>
When I try to create a function that sets the visibility style of the div to hidden, I get an error msg saying, “Cannot set property ‘visibility’ of undefined” but I don’t see why it would be undefined?
function hideOneSlider() {
console.log($("#indicatorType").val());
console.log($("#mapSlider").style);
if ($("#indicatorType").val() === "AccAndAgent") {
$("#mapSlider").style.visibility = "hidden";
}
}
It seems that both of your divs have the same id: selections. Then in your if statement, you are trying to access an element with the id “mapSlider”. “mapSlider” is set as the class on your div, not the id.
Then I added to code I already had that checks for changes in #indicatorType:
$("#indicatorType").change(function() {
//a bunch of code to which I added the following:
if ($("#indicatorType").val() === "MFmapDistrict" || "MFmapProvince") {
$(".mapSlider")[0].style.visibility = "visible";
$(".areaSlider")[0].style.visibility = "hidden";
console.log("Hiding area slider");
} else {
$(".mapSlider")[0].style.visibility = "hidden";
$(".areaSlider")[0].style.visibility = "visible";
console.log("Hiding map slider");
}
}
The key here was that I needed to set visibility property of $(".mapSlider")[0].style not $(".mapSlider").style since I am using a class selector not id selector.
Now I’m running into a new problem which I may be back to ask help for but this issue can be closed. Yay.
Just to be clear. It is not because it’s a class selector, it’s because of how jQuery works.
The $('selector') always returns a jQuery object. If you want to get to the element to use native methods or directly access properties on it you have to index to it.
Depends on what the condition is. If you want to hide the element when it’s empty, look at the CSS :empty pseudoclass in conjunction with display: none.