1

Trying to hide DIV if another DIV is active or visible with css/js. Basically if an item is out of stock I would like to hide my custom div.

<script type="text/javascript">
require(['jquery'], function(jquery) {
    jquery(document).ready(function(){
        if(document.getElementByClass("stock.unavailable").style.visibility == "visible") {
    document.getElementByClass("left-stock").style.visibility = "hidden";
}
      
});
});
</script>

2 Answers 2

0

@shaun try the below code

$(document).ready(function(){
  if(!$('.stock.available').is(":visible"))
  {
    $('.left-stock').remove(); // you can also use .hide()
  }
});
0

It looks like you're using jQuery, so let me provide you with a corrected version of your code i hope useful this:

<script type="text/javascript">
require(['jquery'], function($) {
    $(document).ready(function(){
        if($(".stock.unavailable").is(":visible")) {
            $(".left-stock").css("visibility", "hidden");
        }
    });
});
</script>
  • Replaced document.getElementByClass with $(".stock.unavailable") to use jQuery's selector for elements with the specified class.
  • Used the is(":visible") method to check if the element with class "stock unavailable" is visible.
  • Changed document.getElementByClass("left-stock").style.visibility to $(".left-stock").css("visibility", "hidden") to set the visibility of the element with class "left-stock" to "hidden".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.