tags:

views:

65

answers:

2

I try to remove one box i append in jQuery, but i can't get it removet after i add, sombody can tell me what i dit worng?

    function appendBox( id )
    {
        $("#listContainer").append("<div id=\"appendbox["+ id +"]\"><a href=\"javascript:removeBox("+ id +");\">remove</a></div>");
    }
    function removeBox( id )
    {
        $("#appendbox["+ id +"]").slideUp();
    }
+1  A: 
function appendBox( id )
{
    $("#listContainer").append("<div id=\"appendbox-"+ id +"\"><a href=\"javascript:removeBox("+ id +");\">remove</a></div>");
}
function removeBox( id )
{
    $("#appendbox-"+ id ).slideUp();
}

#appendbox[something] does not mean "the element of id appendbox[something]" but a lot of different things depending on something.

Julian Aubourg
Tanks a lot. what its my bug? :)
NeoNmaN
+1  A: 

For a fully working script you need to wrap the parameter to removeBox in single quotes because it wants a string: javascript:removeBox('"+ id +"'); otherwise it tries to send a variable which doesn't exist.

function appendBox( id )
{
    $("#listContainer").append("<div id=\"appendbox-"+ id +"\"><a href=\"javascript:removeBox('"+ id +"');\">remove</a></div>");
}

function removeBox( id )
{
    $("#appendbox-"+ id ).slideUp();
}
Manticore