Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery appending without removing

So I need to grab content from a specific class and put it in a div, which I use append for...my issue is that append removes the item I append, and I need it to stay there, Here is my code:

$(document).ready(function(){
    var $content = $('#popupcontent');
    var $window = $('#popupwindow');
    $('.open').click(function(){
        //alert('runnning');
        var a = $(this).contents('span');
        $content.append(a);
        $window.fadeIn(300);
    });
    $('.close').click(function(){
        //alert('running');
            var a = $content.contents('span');
        $window.fadeOut(300);
        $('#popupcontent span').remove();
    });
});

So how can I get the content, when clicked, from each .open span to the #popupcontents id without removing it from the .open class?

To show you what I mean: JSFIDDLE

NOTE: the second time you click a link, it wont append any content because that content has been removed from that class, which is not what I want

NOTE2: I cannot simply just append instead of remove in the $('.close').click function because I cannot detect which instance of the .open class the content came from.

like image 250
Ryan Saxe Avatar asked Jun 14 '13 04:06

Ryan Saxe


People also ask

How to append using jQuery?

jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.

How do I append to innerHTML?

To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.

How to add a child element in jQuery?

append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use . prepend() ). The . append() and .

What is prepend in jQuery?

The prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.


1 Answers

You need to clone the element and append the clone:

$('.open').click(function(){
    //alert('runnning');
    var a = $(this).contents('span');
    $content.append(a.clone());
    $window.fadeIn(300);
});

Demo

like image 54
mishik Avatar answered Sep 30 '22 17:09

mishik