tags:

views:

56

answers:

4

Hi, I am a jQuery noob and seems I always will be, been trying to get this simple thing to work.

What I want is, when the checkbox is checked, hide the div with display: none;.

Here's a jFiddle example of what I'm making a mess of.

Thanks for any help, or other solutions :)

A: 

Try the following code:

$(function(){
    $('#checkbox4').change(function() {
        $("#checkout-shipping-address").toggleClass("show-hide");
    });
});

You need to bind the change or click event in order to react.

elusive
+3  A: 

There is no need for a show-hide class. Just pass a boolean to toggle, and set the initial state of the div by using triggerHandler, which lets you fire an event handler bound to an element without actually affecting the state of the element:

$(document).ready(function() {
    $("#checkbox4").click(function() {
        $("#checkout-shipping-address").toggle(this.checked);
    }).triggerHandler('click');
});

Demo: http://jsfiddle.net/nQnDG/3/

karim79
+1  A: 

I agree with karim if you just need to hide/show, use .toggle(bool), if your example is simplified and you need a class, use .toggleClass("class", bool), like this:

$(function(){
  $("#checkbox4").change(function() {
    $("#checkout-shipping-address").toggleClass("show-hide", this.checked)
  }).change();
});​

You can test it out here, the last .change() call is to make the state match when the page first loads, here's what I mean.

Nick Craver
Wow, thanks! Great explanation and thanks for example as well. The change call is exactly what I was after. :)
Kyle Sevenoaks
I was under the impression that IE does not handle change on checkboxes, and it does not necessarily get fired when using the spacebar to check/uncheck in some browsers. I would instead use `click` and set the initial state with `triggerHandler('click')` instead. Please see my answer.
karim79
@karim79 - That's the first I heard about IE and `change`...it had issues with *bubbling* a `change` event prior to 1.4.2 (causing `.live()` issues), but nothing on the actual checkbox that I've seen or heard of. My demo above works in IE8/7 without any issue...I don't have 6 handy to test, but I don't see how it'd fail, `onchange` is at a pretty basic level in the API :)
Nick Craver
I think it's a pre-jQuery 1.4 thing: "As of jQuery 1.4 the change event now bubbles, and works identically to all other browsers, in Internet Explorer."
karim79
Unfortunately, I just added this to my live site ands tested it in IE8 (We don't offer IE6 or 7 versions anymore) and it didn't change the state. Karim's answer works, but the checkbox is loaded checked, so it handles the effect in reverse.
Kyle Sevenoaks
@karim79 - Right, that's what I was talking about :) `change` itself is normalized, you can see the code here: http://github.com/jquery/jquery/blob/master/src/event.js#L745
Nick Craver
@Kyle - You have some other issues happening then, do you have a link I can see? It's best *not* to use `click` because of the event order of `click` vs `change`, especially for `<form>` submission.
Nick Craver
I have a live link: www.euroworker.no but you'll have to add an item to the basked with "Kjøp" buttonm, then click "Handlevogn" then "Gå Videre", then it's the checkbox under "3 Fakturasadresse".. Bit of a lengthy setup, sorry..
Kyle Sevenoaks
@Kyle - just flip the boolean within the handler if the reverse is what you're after? `.toggle(!this.checked)` ?
karim79
@Kyle - I'm getting JavaScript errors on the page in all browsers...this will definitely break things "this.nodes.cart is null or not an object" and "null is null or not an object"...you need to fix those 2 errors, then you shouldn't have any issues.
Nick Craver
Ugh, again? I just updates the ecommerce package we're using to run our shop and now it throws up more errors. Thanks Nick :)
Kyle Sevenoaks
@Nick, how do you get a personalised jsfiddle address ("jsfiddle.net/nick_craver/xxxxx")? I realise this is kinda off-topic, but I can't see any way to register an account or anything...
David Thomas
@David - there will be an open beta soon when they get the registration system up, Piotr was kind enough to create me an account manually a while ago :)
Nick Craver
@Nick, that personalised jsfiddle name is awesome. I can't find the way to solve these errors, gonna have to mail the support guy from the original package. Thanks for your help, at least to solution works in real browsers. :) Thanks for your help.
Kyle Sevenoaks
@Nick Craver, that's a relief. If nothing else it means I can save some money *not* going to my optometrist...guess I just need to network more =b
David Thomas
A: 

Why your code doesnt work is because you dont understand the basics of jQuery.

If you want that, on a click, something will happen, you need to BIND an EVENT to it. your code is not bad but it never gets executed because you didnt bind it.

karim7's code works fine. because he is binding it to the checkbox.

Stefanvds
why do i get a -1 for trying to help this guy understand the basics of jQUery which he cleadly didnt...
Stefanvds