Q:

Jquery/CGI : checkbox append/remove div

I'm trying to add and remove divs using checkboxes. 
Some help for this script would be appreciable :
$(document).ready(function() {

            $('input:checkbox').change(function(){
                 if( $(this).is(':checked') ){
                        var nom = $(this).attr("value");
                        $('body').append($("<div id="+nom+">Switch "+nom+"</div>"));
                }else{
                        $("#" + nom).remove();
                }
        });

In the else, remove() does not work, there is a problem with the selector, don't know how to do with a variable, I searched on the web and did not find anything. 
My second problem is for the append, it works, I can add a new div, but I'm using Perl/CGI, I would like to add a div made by : my $switch1 = SwitchGUI->new("switch-rdc-7", 24, 2); (that make an html reprensentation of a switch). 
If someone could help me,
Thanks. 

A:

As you only give a value to the nom variable inside the "if-clause", that variable will always be undefined inside the "else-clause".  So that means you select "#undefined", which doesn't exist, so nothing is removed. 
I think it will work if you put the initialization (not only the definition, as that happens automatically) of your variable outside of the if/else construct like this:
$(function() {
    $('input:checkbox').change(function(){
        var nom = $(this).attr("value");
        if ( $(this).is(':checked') ) {
            $('body').append($("<div id="+nom+">Switch "+nom+"</div>"));
        } 
        else {
            $("#" + nom).remove();
        }
    });

As for your second problem: 
I'm not familiar with that server side 'switch' thing you're talking about.  But as your javascript is operating on the client, you just have to recreate what that SwitchGUI is creating.  E.g. first create your GUI using that SwitchGUI thing, then look at the generated html code for that, and exactly recreate that in your javascript where you create your div now.