0

I am appending div in existing div and then one more div into append div but it not work for me. what am I doing wrong. please help

<head> <script type="text/javascript"> $(function(){ $('a').click(function(){ var cl=$('#free').clone(); var jj=$('.append') var mm=jj.append('<div class="hii"></div>') mm.append(cl) }) }) </script> </head> <body> <div style="background:#F00; width:500px; height:50px" id="free"></div> <a href="#">hide</a> <div class="append"></div> </body> 
9
  • That's not the solution, but you're missing a lot of ;. Commented Aug 22, 2012 at 12:35
  • ; is optional in javascript. And above code is working at my end. Commented Aug 22, 2012 at 12:36
  • You're cloning an element with the id 'free', so you're creating an element that doesn't have an unique identifier. This isn't valid HTML, so avoid doing that. Instead of id="free", do class="free". Also, what is going wrong with this? Can you create a fiddle or something like that which demonstrates this problem? Commented Aug 22, 2012 at 12:36
  • 2
    It is optional, but do you really want to leave it off?? Commented Aug 22, 2012 at 12:41
  • 1
    Yes its a good practice to always use ;. Its a good programming habit. :) Commented Aug 22, 2012 at 12:50

1 Answer 1

2

jj.append('<div class="hii"></div>') return jj, not new div. So use this:

$(function(){ $('a').click(function(){ var cl = $('#free').clone(); var jj = $('.append'); var mm = $('<div class="hii"></div>'); jj.append(mm); mm.append(cl); }); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

@SperanskyDanil & Amit: I can see the same output from both functions.
@SperanskyDanil: I have seen that but the question is if his code is wrong, then why its giving the correct output? Could you please explain.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.