Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery appends a paragraph twice

Tags:

jquery

I'm creating a jquery animation and weirdly, when I append a new paragraph, it appends it twice, the problem only happens when 2 previous paragraphs are appended, probably that's a jQuery bug.. otherwise, I hope someone can help me.

HTML:

<body>
    <div id="element">
     </div>
</body>​ 

JavaScript:

$(function() {
    animation();
});

function animation()
{
   $('<p id="p1">paragraph 1</p>').appendTo('#element');
   $('<p id="p2">paragraph 2</p>').appendTo('#element');
   $('#p1, #p2').fadeOut(600, function(){
       $('#p1, #p2').remove();
       var $p3 = $('<p>paragraph 3 </p>').appendTo('#element');
   });
}​

The problem is with "paragraph 3" that is appended twice! You can run the code here: http://jsfiddle.net/jonah13/QLM2s/

Thanks, Younes

like image 709
jonah13 Avatar asked May 07 '12 07:05

jonah13


People also ask

How to add paragraph using jQuery?

Create a paragraph element with some text and append it to the end of the document body using jQuery. JavaScript Code : $( "p" ). add( "<span>Exercises</span>" ).

How to append text in 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 to append paragraph in div using jQuery?

It's easy to append text into this div using append() as shown below. It will append the text directly before the closing </div> tag. This example would add "this text was appended" to the above div: $('#example').


3 Answers

I don't know why other answers that identified the problem were down-voted, since they identified the problem.

Here is the code to fix it. Note from the jQuery docs, that as of jQuery 1.6 (which I assume you're using since the jsFiddle was using 1.7.2) you can use .promise():

As of jQuery 1.6, the .promise() method can be used in conjunction with the deferred.done() method to execute a single callback for the animation as a whole when all matching elements have completed their animations.

I've updated your fiddle:

$(function() {
   animation();
});

function animation()
{
   $('<p id="p1">paragraph 1</p>').appendTo('#element');
   $('<p id="p2">paragraph 2</p>').appendTo('#element');
   $('#p1, #p2')
     .fadeOut(600)
     .promise().done(function() {
       $('#p1, #p2').remove();
       var $p3 = $('<p>paragraph 3 </p>').appendTo('#element');
   });         
}​
like image 119
Rob Cooper Avatar answered Sep 20 '22 15:09

Rob Cooper


The function provided to fadeOut() is fired once per element which matches the selector. Since the selector returns two elements it is fired twice, thus appending two paragraphs.

Modified example here: http://jsfiddle.net/QLM2s/1/

like image 28
Tim M. Avatar answered Sep 16 '22 15:09

Tim M.


The problem is where fade out paragraphs 1 and 2: jQuery is executing the code inside the fade-out function for every selector you give it (in this case, two paragraphs so two appends).

like image 26
BenjaminRH Avatar answered Sep 20 '22 15:09

BenjaminRH