1

I am trying to show or hide a div called "contact-form" on website based on another divs a css class, which is connected to whether a product is in or out of stock.

It needs to show the div if the class is "in-stock", and hide if it's class is "out-of-stock"

any ideas? struggling to figure it out!

<div id="contact-form"></div>

<p class="stock in-stock"></p> <!-- if product is in stock this shows-->

<p class="stock out-of-stock"></p> <!-- if out of stock this show-->

<script>

    if ('p.in-stock') {
        ('#contact-form').show();   
    }
    else {
        ('#contact-form').hide();
    }

</script>

Website is here - http://trent-art.co.uk/shop/barnes-david-still-life-of-flowers/ - out of stock product which needs the "submit best offer" button hiding if out of stock.

5
  • 1
    Add your HTML code also Commented Jun 9, 2015 at 14:14
  • 1
    This question is a little short on information. Can you share what you have tried, and what problems you have run into? Commented Jun 9, 2015 at 14:14
  • jQuery makes this easy. Commented Jun 9, 2015 at 14:15
  • this should help you w3schools.com/jquery/eff_toggle.asp it shows you on to hide/reveal by click, you can then adapt it to your needs Commented Jun 9, 2015 at 14:18
  • Show/Hide the div, or the <p> tag? Commented Jun 9, 2015 at 14:18

4 Answers 4

3
$('#contact-form').toggle($('p.stock').hasClass('in-stock'));

OR

if ($('p.stock').hasClass('in-stock')) {
    $('#contact-form').show();
} else {
    $('#contact-form').hide();
}
Sign up to request clarification or add additional context in comments.

4 Comments

He didn't mention that he uses jQuery.
i have added this to my site and it didn't work <script> $(document).ready(function() ({ if ($('p.stock').hasClass('in-stock')) {$('#contact-form').show(); } else { $('#contact-form').hide(); } }); </script>
@cookney Make sure you've loaded jQuery before this, and all the elements used above are unique on the page
@tushar sorry still not working, can see my code right at bottom of the page on trent-art.co.uk/shop/barnes-david-still-life-of-flowers - best offer button remains
2

Since you didn't tag your question with jQuery, here is a vanilla JS alternative:

var formDiv = document.getElementById("contact-form");
var stockPara = document.getElementsByClassName("stock");
var sStockClass = stockPara[0].className;
formDiv.style.display = (sStockClass.indexOf("in-stock") < 0) ? "none" : "block";

This code could be shortened into fewer lines (technically, you could do it in one), but I left it broken up for clarity. The "consolidated" version would be:

document.getElementById("contact-form").style.display =
    (document.getElementsByClassName("stock")[0].className.indexOf("in-stock") < 0) ?
    "none" : "block";

If you're not familiar with ternary operators, that is what I used in the last line . . . it's basically a one-condition, two-option, one-line, if statement. The code says, "if 'in-stock' is in the sStockName string value, then assign 'block' as the display value, otherwise, use 'none'"

More info on ternaries here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Comments

1

in pure Js it's:

var p= document.querySelector("p")

 if (p.className.match(/\bin-stock\b/)) {
document.querySelector("#contact-form").style.display = 'block';
}else{
document.querySelector("#contact-form").style.display = 'none';
}

fiddle

fiddle2 ---the one with display block---

1 Comment

@cookney strange; in my example it's working.. look at fiddle
0

if there is 1 contact form per product then put div of contact form inside product and hide with css :

.in-stock + .showOrHide
{
    display:block;
}

.sold + .showOrHide
{
    display:none;
} 

https://jsfiddle.net/o379yqy5/1/

2 Comments

i want to hide the contact form if the product is out of stock, so the above example doesnt work.
Then your example is not clear because there is 1 form but 2 products in your example ... 1 in-stock and 1 out-of-stock, what should the result be for your example ? Is there a contact form per product ? or is there only 1 product and 1 contact form normally ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.