tags:

views:

74

answers:

4

hello guys,

am trying to remove a div inserted by jquery onclick. for some reason its just not working i tried remove(); hide(); css(); empty(); they all are just not working.. so here's my js function

function callme()
{
var content = '<div id="floating_box" class="fb">'
'sdfsdfsdfsdfsfsddsfsfsdfsdfsdfsfsdfsdfsdfsfsdfsd'
'</div>';
jQuery('body.floating_box').empty();
jQuery('body').append(content);
}

and here is my html code

<div id="notify" class="nn" onclick="callme()">
 <span id="nbox" class="nb">
<img src="gn16.png">
<span id="ntext" class="nt">23</span>
</span>
</div>
A: 
var content = '<div id="floating_box" class="fb">' +
'sdfsdfsdfsdfsfsddsfsfsdfsdfsdfsfsdfsdfsdfsfsdfsd' +
'</div>';

See the + ??

jQuery('body.floating_box').remove(); // edit: sorry my mistake
jQuery('body #floating_box').remove(); // better? should remove all elements within <body> with the id "floating_box"

Should be just fine.

opatut
the OP is emptying the body, and now you are removing the body... wow!
Reigel
No, it shouldn't. `body.floatingbox` means "all the bodies with the class floatingbox".
MvanGeest
its still not working
clonex1
@clonex1 we need more codes, and more explanations..
Reigel
But there's only one `body`, and it presumably doesn't have the `floatingbox` class, so the `remove()` does nothing.
Peter Ajtai
i tried all the possible id/class combination that is possible and not possible. but its just not working. here's the new js code after adjustment:function callme(){ var content = '<div id="floating_box" class="fb">' + 'sdfsdfsdfsdfsfsddsfsfsdfsdfsdfsfsdfsdfsdfsfsdfsd' + '</div>'; jQuery('.fb').remove(); jQuery('body').append(content);}and its still not working too... i spent that last 3 hours trying to remove this div its just dont wanna go
clonex1
You're right, it should be "body #floating_box"
opatut
@opatus, i tried that too... no lucku can check the code that am trying to get to work @ http://passivenet.com/2/
clonex1
hey it works...you remove the blue box with "sdfsdfsdf,..." and add another one with exactly the same content....Try to insert some random stuff, you'll see it changes everytime you click the link
opatut
@opatut, that's not the purpose of remove isn't it!i just need to have that div vanishes on the second click
clonex1
+2  A: 
Gaby
But he **hasn't even inserted the `content` string in the DOM**! Why should that ID exist?
MvanGeest
@MvanGeest, he wants to be able to remove previously added content, and then re-enter it ... (*it is on a click event*)
Gaby
Could be... but there's no `#floating_box` in his HTML example.
MvanGeest
yeah, i fixed that part. even so its still not removing
clonex1
Not in the order you're executing it... you're first removing it, then appending it...
MvanGeest
@clonex, use `jQuery('#floating_box').remove();`
Gaby
There's no `#floating-box` in the code, since it was inserted with Javascript.
Peter Ajtai
@Gaby, just tried it 3 times... its not removing it
clonex1
@clonex1, your code is re-adding what you remove.. what *exactly* do you want to happen when you click ?
Gaby
@clonex1 it is... you are just hypnotized... when it was removed, another one was added... see this demo to illustrate http://jsfiddle.net/QvBmC/
Reigel
here's the link to the code am working onhttp://passivenet.com/2/index.html
clonex1
@Gaby, when i click in the first time content is appened (which is nice thing)when is reclick to have it removed - its not removing just stays there -
clonex1
@clonex1, it does not jsut stay there .. it is removed and re-added.. you want to show it on one click and hide it on the next ? .. why did you never mention that :) ?!?
Gaby
@clonex1, updated answer with solution..
Gaby
+1  A: 

demo

function callme()
{
    var content = '<div id="floating_box" class="fb">' +
    'sdfsdfsdfsdfsfsddsfsfsdfsdfsdfsfsdfsdfsdfsfsdfsd' +
    '</div>';
    jQuery('#floating_box').toggle();
    if (!$('#floating_box').length)
      jQuery('body').append(content);
}
Reigel
@Reigel, just tried your edited code on my local/remote site http://passivenet.com/2/, its still not working
clonex1
but you see the demo worked?
Reigel
@clonex1 in you site you have misspelled `length`, edit it and it should work..
Gaby
that worked. thanks Gaby thanks everyone for your helpsaved my day
clonex1
A: 

You are not seeing the floating_box being removed because a new version of the floating_box div is added whenever you call the callme function.

 function callme() {
     var $floatingbox = jQuery("#floating_box");
     if ( $floatingbox.length ) {
         $floatingbox.remove();
     }
     else {
        var content = '<div id="floating_box" class="fb">' +
            'sdfsdfsdfsdfsfsddsfsfsdfsdfsdfsfsdfsdfsdfsfsdfsd' +
            '</div>';  
        jQuery('body').append(content);  
     }
 }

This will check whether the floating_box div exists, and remove it otherwise add it.

Mario Menger