3

I'm a first timer on here, so I apologize for inadvertently breaking rules. I am trying to create a series of buttons that, if clicked, add certain values to a url. The idea is I want to find out which of 2 variables need to be passed when the person clicks the link at the end.

Here's my code:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
jQuery(function(){
  var product1 = 'productId=1&productQuantity=1';
  var product2 = 'productId=2&productQuantity=1';
  var carturl = 'http://cart.net?clearCart=true';
  jQuery(function(){
    jQuery("#Product1Yes").click(function () {
      jQuery("#Product2").show("fast");
      jQuery("#Product1").hide("fast");
    });
    jQuery('a#upsell').attr('href', function() {
      return carturl + '&' + product1;
    });
    jQuery("#Product1No").click(function () {
      jQuery("#Product2").show("fast");
      jQuery("#Product1").hide("fast");
    });
    jQuery("#Product2Yes").click(function () {
      jQuery("#Product3").show("fast");
      jQuery("#Product2").hide("fast");
    });
    jQuery('a#upsell').attr('href', function() {
      return carturl + '&' + product2;
    });
    jQuery("#Product2No").click(function () {
      jQuery("#Link).show("fast");
      jQuery("#Product2").hide("fast");
    }); 
  });
});
</script>

<div id="Product1">
    <button class="btn btn-primary" id="Product1Yes">Yes</button>
    <button class="btn btn-danger" id="Product1No">No</button>
</div>

<div id="Product2">
    <button class="btn btn-primary" id="Product2Yes">Yes</button>
    <button class="btn btn-danger" id="Product2No">No</button>
</div>

<div id="Link">
    <a id='upsell' href='#'>Click here to check out</a>
</div>

I think I'm missing something dumb because my show/hide functions aren't working and I don't think the link is getting created right. Thoughts?

3
  • @adeneo - that is is in the carurl varaiable Commented May 26, 2013 at 16:06
  • @Hogan - The return returns the value to the attr() method, setting the href. Commented May 26, 2013 at 16:07
  • 1
    Right now the code is binding clicks inside clicks etc. You should indent your code properly, and you'll notice errors like that right away. Commented May 26, 2013 at 16:09

1 Answer 1

1

jsFiddle here.

<div id="Produc1"> should be <div id="Product1"> in your HTML.

Also, you were missing a few closing braces:

var product1 = 'productId=1&productQuantity=1';
var product2 = 'productId=2&productQuantity=1';
var carturl = 'http://cart.net?clearCart=true';
jQuery(function(){
  jQuery("#Product1Yes").click(function () {
    jQuery("#Product2").show("fast");
    jQuery("#Product1").hide("fast");
  });
  jQuery('a#upsell').attr('href', function() {
    return carturl + '&' + product1;
  });
  jQuery("#Product1No").click(function () {
    jQuery("#Product2").show("fast");
    jQuery("#Product1").hide("fast");
  });
  jQuery("#Product2Yes").click(function () {
    jQuery("#Link").show("fast");
    jQuery("#Product2").hide("fast");
  });
  jQuery('a#upsell').attr('href', function() {
    return carturl + '&' + product2;
  });
  jQuery("#Product2No").click(function () {
    jQuery("#Link").show("fast");
    jQuery("#Product2").hide("fast");
  });
});
Sign up to request clarification or add additional context in comments.

6 Comments

#Product3 is not defined
@Hogan I was guessing he hasn't created that part in the HTML yet
I've added that now, but it still doesn't seem to be working.
@bertlymartizzle Where have you added it? I don't see it in your question? Obviously it won't work for Product3 as you haven't defined it or given me the code for it, but the other two parts work as shown in my jsFiddle!
My bad. I'm still learning what I'm supposed to do on these. Thanks for your help
|

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.