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 - / - out of stock product which needs the "submit best offer" button hiding if out of stock.
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.
Share Improve this question edited Jun 9, 2015 at 14:49 FoamyMedia asked Jun 9, 2015 at 14:14 FoamyMediaFoamyMedia 4963 gold badges14 silver badges33 bronze badges 5-
1
Add your
HTML
code also – Tushar 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? – Jay Blanchard Commented Jun 9, 2015 at 14:14
- jQuery makes this easy. – T McKeown Commented Jun 9, 2015 at 14:15
- this should help you w3schools./jquery/eff_toggle.asp it shows you on to hide/reveal by click, you can then adapt it to your needs – keikoku92 Commented Jun 9, 2015 at 14:18
-
Show/Hide the div, or the
<p>
tag? – LcSalazar Commented Jun 9, 2015 at 14:18
4 Answers
Reset to default 3$('#contact-form').toggle($('p.stock').hasClass('in-stock'));
OR
if ($('p.stock').hasClass('in-stock')) {
$('#contact-form').show();
} else {
$('#contact-form').hide();
}
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/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
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---
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/o379yqy5/1/