Jquery problem with "remove" please help...

Discussion in 'jQuery' started by abi, Jan 7, 2009.

  1. #1
    Hi, can anyone explain why this does not work?

    I have the following on page:

    <div id="bob">hello</div>


    Now in my javascript file i have this:

    var i = "bob"
    var tmp = document.getElementById(i)
    $(tmp).remove();

    Ok? the above seems to work fine. When i remake the div with the same id: "bob" again, it is a new div.

    BUT when i try this:


    var i = "bob"
    var tmp = document.getElementById(i)
    $(tmp).slideUp("slow")(function (){
    $(tmp).remove();
    }
    )

    It slides up, and appears to remove, but when the div is remade with the same id: "bob", the old one appears to come back, with all the old data in it.

    Can anyone help?

    Many thanks!!! :p
     
    abi, Jan 7, 2009 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You're not setting the callback function properly in your slideUp function. You don't close the parenthesis then open a new one, you simply put a comma.

    var i = "bob"
    var tmp = document.getElementById(i)
    $(tmp).slideUp("slow", function (){
    $(tmp).remove();
    });

    By the way, you're already using the awesome power of jQuery, why would you use document.getElementById?! Just do $("#bob") :)
     
    zerxer, Jan 7, 2009 IP
  3. abi

    abi Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks zerxer! :)

    I havent tried your code yet, but it makes sense.


    So would i do this? $("#" + var) e.g: $("#" + i)

    Also, is there anyway to replace this:

    for(i=0;i<10;i++){

    }

    with a jquery function?


    I'm just getting started, im so used to writing my own javascript, its hard to get going with jquery :)

    Thanks for the help :p
     
    abi, Jan 7, 2009 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, $("#" + i) would work fine. If you want to reference by class name, you'd use a period instead of pound (#) symbol.

    Eh, there is a looping function in jQuery, but it's for looping through objects/json object.

    $.each(something, function(){//do something with 'this'});

    where var something = $("div") (to fetch all divs) or where var something = {name1: 'value', name2: 'value'}; (your own JSON object).

    The documentation on jQuery's site is very useful for easily learning all the functions they offer. Also, if you ever need help again with jQuery, I recommend trying out their IRC chat (if you have an IRC client) as it always has people in it with a few that are active at any given time and they can help you. I sometimes join that chat to try to answer people's questions.
     
    zerxer, Jan 7, 2009 IP