Append and Slide together jQuery
Asked Answered
P

3

28

I have this append method which I made to add more input boxes until there is 10 of them which will disable into making more.

i = 0;
$('#add-link').click(function() 
{   
    if(i < 9)
    {
        $('.insert-links').append('<div class="new-link" name="link[]"><input type="text" /></div>');
        i++;
    }
    if(i == 9)
    {
        $('#add-link').html('');    
    }
});

Although, it's good. However, I want to implement a slideDown when appended, I've tried doing this:

$('.insert-links').append('<div class="new-link" name="link[]"><input type="text" /></div>').slideDown("fast");

Which doesn't work at all.

Polysynthetic answered 19/9, 2010 at 22:12 Comment(0)
W
36

Like SimpleCoder's solution, but in only one line using appendTo():

$('<div style="display: none;" class="new-link" name="link[]"><input type="text" /></div>').appendTo($('.insert-links')).slideDown("fast");

Demo: http://jsfiddle.net/V4SVt/336/

Wood answered 3/1, 2013 at 18:29 Comment(2)
being in one line just loses readability.. not sure what you're gaining with thatFeuar
The one-line aspect is much less important that the .appendTo() use. That is the key change.Virga
L
39

append() returns a reference to the original selector, not what was appended. I think you are looking for this:

$('.insert-links').append('<div style="display: none;" class="new-link" name="link[]"><input type="text" /></div>')
$('.insert-links').find(".new-link:last").slideDown("fast");

Live demo:

http://jsfiddle.net/V4SVt/2/

Lentz answered 19/9, 2010 at 22:19 Comment(0)
W
36

Like SimpleCoder's solution, but in only one line using appendTo():

$('<div style="display: none;" class="new-link" name="link[]"><input type="text" /></div>').appendTo($('.insert-links')).slideDown("fast");

Demo: http://jsfiddle.net/V4SVt/336/

Wood answered 3/1, 2013 at 18:29 Comment(2)
being in one line just loses readability.. not sure what you're gaining with thatFeuar
The one-line aspect is much less important that the .appendTo() use. That is the key change.Virga
H
3

Although SimpleCoder's solution is perfectly valid, I'd do it like this:

i = 0;
$('#add-link').click(function() {   
    if(i < 9) {
        $('.insert-links').append('<div style="display: none;" class="new-link link_' + i + '" name="link[]"><input type="text" /></div>');
        $('.link_' + i).slideDown("fast");
        i++;
    }
    if(i == 9) {
        $('#add-link').fadeOut('200');
    }
});

The reason for it would be that each link input would get an ID in the form of a second class, which would come very handy for selecting them in case one wants to remove an element, should one accidentally add more than one needs. I have also added a fade out effect on the add-link element so the user doesn't get confused that it just disappeared. Of course, it is just a matter of personal taste, but I find it more user-friendly.

Hope this helps.

Hagiocracy answered 19/9, 2010 at 22:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.