Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending an HTML div inside another div

I would like to add another div within a div group on a button click in order to have a continuously generating slider. Like so:

<div class="carousel-items">
    <div class="date-carousel-other slider"></div>
    <div class="date-carousel-other slider"></div>
    <div class="date-carousel-other slider"></div><!-- new div -->
</div>

Currently my java script is:

$('.slick-next').click(function(){
    $('"<div class="date-carousel-other slider"></div>"').append( ".carousel-items" );
});

The result is appending the boxes underneath my slider (big div) instead of inside the slider. Appending to ".slider" instead, duplicates all the div's and wedges boxes inside the slider.

Is my syntax off or am I using the wrong method?

like image 201
jShoop Avatar asked Mar 15 '23 17:03

jShoop


1 Answers

As per Docs,

The .append() and .appendTo() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With .append(), the selector expression preceding the method is the container into which the content is inserted. With .appendTo(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.

So your solution failed because .append() expects the target as selector and content as argument in method .append(). Like this,

$('.slick-next').click(function(){
  $(".carousel-items").append('<div class="date-carousel-other slider"></div>' );
});

if you want to keep the target and content in same way,use .appendTo() instead of .append():

$('.slick-next').click(function(){
 $('<div class="date-carousel-other slider"></div>').appendTo( ".carousel-items" );
});
like image 107
Milind Anantwar Avatar answered Mar 23 '23 21:03

Milind Anantwar