susan
susan

Reputation: 382

Hide/Show Accordion based on javascript functions

I have an accordion with one panel I would like to only show if certain criteria are met. The panel id is "product-options".

I have a couple javascript functions, one is to verify states from a dropdown (dropdown id="province") and the other is to verify age with a checkbox (checkbox id="age_verification"). The accordion panel "product-options" is activated with a header, id="BuyThis".

What I would like is to only show the panel "product-options" when the states match the list.

Here is my script as is:

<script>
// Validates State is in list of allowed for wine shipping...  
function validateState() {
      // Confirms province is allowed for wine shipping     
          var states = ["Alaska", "Arizona", "California", "Colorado", ""];
                 if ($.inArray($("#province").val(), states) <0) {
                 alert("Shipping gifts containing alcohol to this state is prohibited by law. Please choose another item.");
                 return false;       
        }
alert("Item Available! Please choose your options.");
return true;
}
    // Validates age verification is checked...   
    function validateAge() {    
        if (!$('#age_verification').is(':checked')) {
            alert("Please verify you are 21 years of age or older.");
            return false;       
        }    
return true;
}
</script>

Corresponding HTML:

<h3 id="BuyThis" onclick="return validateState();"><button class="Button buyThisBtn"><i class="icon-shopping-cart"></i>&nbsp;&nbsp;Buy This!</button></h3>              
<div class="ProductContainer" id="product-options"> 
    <form>
    [BODY OF ACCORDION PANEL WITH A BUNCH OF IRRELEVANT CODE......]     

             <button id="addtocart" type="submit" class="Button" onclick="validateAge(); return validateState();"><i class="icon-shopping-cart"></i> ADD TO CART</button>                   
    </form>
</div>

I tried incorporating the following to no avail:

$(function () {
    $("#buyThis").change(function () {
    if ($.inArray($("#province").val(), states) <0) {                
         alert("Shipping gifts containing alcohol to this state is prohibited by law. Please choose another item.");
         $("product-options").hide();               
    }                
$("product-options").show();             
}

Upvotes: 0

Views: 1733

Answers (1)

Sandcar
Sandcar

Reputation: 892

Try like this:

<h3 id="BuyThis" ><button class="Button buyThisBtn"><i class="icon-shopping-cart"></i>&nbsp;&nbsp;Buy This!</button></h3>              
<div class="ProductContainer" id="product-options"> 
    <form>
    [BODY OF ACCORDION PANEL WITH A BUNCH OF IRRELEVANT CODE......]     

             <button id="addtocart" type="submit" class="Button" onclick="validateAge(); return validateState();"><i class="icon-shopping-cart"></i> ADD TO CART</button>                   
    </form>
</div>


$(function () {
    $("#buyThis").click(function () {

if(validateState()){

       if ($.inArray($("#province").val(), states) <0) {                
         alert("Shipping gifts containing alcohol to this state is prohibited by law.         Please choose another item.");
         $("product-options").hide();               
       }                
    $("product-options").show();
  }             
}

Edit 2

Please see this fiddle with some changes to your code:

Please not that you are using the $.inArray method in the wrong way . In the click event and also in the validateState function i think.

I also made some changes to the html markup

Edit 3

Please see this fiddle with an working example : I hope this could help you now.

html

<select id="province">
    <option value="Alabama">Alabama</option>
    <option value="Alaska">Alaska</option>
    <option value="Arizona">Arizona</option>
    <option value="Arkansas">Arkansas</option>
    <option value="California">Colorado</option>
</select>
         <button id="test" >Teste</button>  
<h3 id="BuyThis" style="background:red;" >

    <button id="buyThisButton" class="Button buyThisBtn"><i class="icon-shopping-cart"></i>&nbsp;&nbsp;Buy This!</button>
</h3>              
<div class="ProductContainer" id="product-options"> 
    <form>
    [BODY OF ACCORDION PANEL WITH A BUNCH OF IRRELEVANT CODE......]    
        <button id="addtocart" type="submit" class="Button" onclick="validateAge(); return validateState();"><i class="icon-shopping-cart"></i> ADD TO CART</button>                   
    </form>
</div>

javascript

$( document ).ready(function() {
  // Handler for .ready() called.

    $("#BuyThis").click(function() {
    //alert("test");

     if(validateState()){
         // if state is valid do this

     }else{
     // do that

     }
  });


}); // end of dom ready



// Validates State is in list of allowed for wine shipping...  
function validateState() {
      // Confirms province is allowed for wine shipping     
          var states = ["Alaska", "Arizona"];
                 if ($.inArray($("#province").val(), states) >= 0) {                     
                 alert("Oh no! Shipping items containing alcohol to this state is prohibited by law. Please choose another item.");
                 //$("#BuyThis").unbind("click");
                 $("#product-options").slideUp(); 
                 return false;       
        }
    alert("Item Available! Please choose your options.");
    $("#product-options").slideDown();  
    return true;
}


    // Validates age verification is checked...   
    function validateAge() {    
        if (!$('#age_verification').is(':checked')) {
            alert("Please verify you are 21 years of age or older.");
            return false;       
        }    
return true;
}

Upvotes: 2

Related Questions