Jump to content
rootKID

jQuery show/hide on change selection option (almost working)

Recommended Posts

so, me and a friend have this website:
https://pizza1.pizzabix.dk/pizzaer/62-117-9-italiano.html#/29-storrelse-almindelig

Currently, im adding CUSTOM JS to the PrestaShop via this file here:

https://pizza1.pizzabix.dk/themes/classic/assets/js/custom.js

At the very bottom of the file, beneath the comment (Product Normal/Family Size on Product Details Page - Show/Hide on change) i have this code here:

$(function () {
  // "29" == (Normal Size)
  // #an_productfields_1_id == (Normal Size)
  // "30" == (Family Size)
  // #an_productfields_2_id == (Family Size)
  // #group_6 == SELECTION/DropDown Form Box

  //$("#an_productfields_1_id").hide(); // Normal Size (hide instantly)
  //$("#an_productfields_2_id").hide(); // Family Size (hide instantly)
  $("#group_6").change(function () {
    if ($("#group_6").val() == "29") {
      // Normal Size
      $("#an_productfields_1_id").show();
    } else {
      $("#an_productfields_1_id").hide();
    }
    if ($("#group_6").val() == "30") {
      // Family Size
      $("#an_productfields_2_id").show();
    } else {
      $("#an_productfields_2_id").hide();
    }
  });
});

and i almost got it working, however! As u try it on the website, and i for example take the (Almindelig) value from the select box, it dosent shows the very first time... but when i change to Family Size, nothing happens on the second choise! And same goes for the other way around. When i select (Familie) on first try, it works, hides the "normal sizes / almindelig", and shows family...

any ideas as to why it does this? I should have used the propper code i think... i got a peace of the code form this linke by searching google:

http://jsfiddle.net/Y3pW9/

https://stackoverflow.com/questions/18572401/jquery-select-change-show-hide-div-event

If you could possibly tell me what im doing wrong and what i could do in future cases, i would be very happy!

Also, i did consider using "toggle" from jQuery, but not sure hos to use that just yet so i went with this option since i got the most experience (so to speak) with this one...

in anyways, i hope you guys can help me out, as always! :D

Thanks in advance!

Looking foreword for a reply... very... VERY... patiently ;)

Share this post


Link to post
Share on other sites

I think you are over complicating with to many if/else conditions, you just require:

  IF equal 29

Show() _1_id

hide() _2_id

ELSE

hide() _1_id

show() _2_id

Edited by dsonesuk

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...