views:

40

answers:

3

I am using this probably ugly javascript to show a text box (in a li tag plus its label) if a checkbox is checked.

   $("#li-2-21").css("display","none");
   $("#Languages-spoken-and-understood-8").click(function(){
    if ($("#Languages-spoken-and-understood-8").is(":checked"))
    {
        $("#li-2-21").show("fast");
    }
    else
    {     
        $("#li-2-21").hide("fast");
    }
  });

That works fine but it doesn't work if a page is loaded and the checkbox is already checked because the #li-2-21 gets automatically hidden.

Do I need to create a function that reads the state of the checkbox? Or is there a simpler way?

Oh and also feel free to shorten that ugly code, I guess there's a shorter way to achieve my goal? Thanks so much for your help!

+1  A: 

Most concisely:

$(document).ready(function() {
   $("#Languages-spoken-and-understood-8").change(function() {
      $("#li-2-21")[$(this).is(":checked") ? 'show' : 'hide']("fast")
  }).change();
});

EDIT : switched from click to change event

Scott Evernden
looks nice but doesn't do anything when you click the checkbox?
Julian
why not ? the event hander is set up upon page load so it'll toggle visibility thereafter .. At page load we bind checkbox click to a function that shows/hides a listitem according to the checkbox state . we also FIRE (via click()) that event handler once so that it sets the display css correctly at the beginningi was missing a paren - maybe you didn't catch that .. i've edited
Scott Evernden
Nearly perfect. Now onload the text field is hidden but the checkbox is checked.And if I set the checkbox to be checked in html, onload the text field appears but the checkbox gets unchecked.
Julian
okay .. use change event instead of click event
Scott Evernden
thanks so much, you're a genius!
Julian
+1  A: 

Extract your click function into a separate function (not inline) and run it on the load of the page:

function ToggleCheckbox() {
    if ($("#Languages-spoken-and-understood-8").is(":checked"))
    {
        $("#li-2-21").show("fast");
    }
    else
    {     
        $("#li-2-21").hide("fast");
    }
}

$(function() {
    $("#Languages-spoken-and-understood-8").click(ToggleCheckbox);
    ToggleCheckBox(); 
});

If you want to clean it up a bit I'd extract the checkbox and languages element into separate vars:

var languages = $("#Languages-spoken-and-understood-8");
var checkbox = $("#li-2-21");

make sure you place them in the appropriate scope though. This will mean that jQuery doesn't need to keep requerying the DOM to find them.

Darko Z
This looks very elaborate and works but the text field isn't hidden onload if the checkbox is unchecked. What am I not understanding here?
Julian
have edited answer. I should have been more specific, in that, the toggle call and the click assignment need to happen once the document is ready. The `$(function() { });` call will make sure that happens.
Darko Z
Sorry I'm really bad at JS but this still doesn't hide the text field onload if checkbox is unchecked?
Julian
A: 

Just call the function immediately upon defining it's function. Example:

   $("#Languages-spoken-and-understood-8").click(function(){
    if ($("#Languages-spoken-and-understood-8").is(":checked"))
    {
        $("#li-2-21").show("fast");
    }
    else
    {     
        $("#li-2-21").hide("fast");
    }
  }).click();
Robert