add and clone HTML element by using jQuery

by Developer 19. January 2010 18:39

Sometimes you need to add one HTML element to another element. By using jQuery, you can easily do this thing.

jQuery framework provides many functions prepend(), prependTo() and clone() functions to full fill this type requirement.

 

<html>
    <head>
        <title>This is jquery example for creating the nodes dynamically</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                //prepend will insert given element at the begining of specified id
                $('#parentid').prepend('<div>This element added using prepend method</div>');
               
                //Similar to prepend(), prependTo() is used for adding DOM nodes dynamically. It inserts the specified
                //element(s) at the beginning of the selected target, where the target can be in the form of an HTML
                //element, a string, or a jQuery object. The method returns a jQuery object. The following is the equivalent
                //of the previous prepend() call.
                //$('<p>append element using prependTo method</p>').prependTo('p');
           
                 //When we want to add a DOM node (which is a copy of an existing element) on the fly, we use the clone()
                 //method. This method makes a copy of the selected element and returns it as a new jQuery object. To
                 //make a copy of the h2 element in the jQuery code and insert it before the paragraph element, the code
                //may be as follows:

               // $('div').clone().prependTo('p');           
           
            });   
        </script>    
    </head>
    <body>
        <div id="parentid">
        This is simple text before using jquery
        </div>
        <p>append element using text</p>
    </body>
</html>

 

$('#parentid').prepend('<div>This element added using prepend method</div>') will insert "<div>This element added using prepend method</div>" element at the begining of the element whose id is "parentid".

 

$('<p>append element using prependTo method</p>').prependTo('p') will insert the specified element or elements at the beginning of the selected target, where the target can be in the form of an HTML element, a string, or a jQuery object.

 

$('div').clone().prependTo('p')  will add a copy of <div> element on the fly. clone() method makes a copy of the selected element and returns it as a new jQuery object.

 

Download source code: prepend and clone.zip (36.05 kb)

Comments

5/9/2010 5:17:21 AM #

T7Plus Manual

Know any other sites other than add and clone HTML element by using jQuery?  It's like Hells Kitchen with Ramsey.  I mean he's with this cooks and merchants and they just dont even know how to run their businesses.  Ya know?

T7Plus Manual

5/13/2010 6:49:39 AM #

Jeffie Heagy

hello it is my first reply on this web and to start with I would like to thank for the great information, which I found in this and all previous topics , it really helped me a lot. I will definitely put this blog on my google reader ;) Also, I would like to ask - don't you mind if I will quate some information from your blog because I am writing articles for the Associated Content, Ezine and other articles directories (this is my part time job)? It would really help me with some of mine articles. Of course, I will mention your blog title or URL (not all articles directories allows URL's , so I can't 100% promise that you will get a direct link to your blog).

Jeffie Heagy

5/13/2010 7:53:03 AM #

Hypercom T7Plus Manual

FYI, your blog isn't loading right in Opera.  It may be your theme?  But other than that, I'm going to say this isnt the first I've seen this.  But yes, it's pretty much the way it is and the way it will be.

Hypercom T7Plus Manual

5/14/2010 10:32:21 AM #

Hypercom T7Plus Manual

I don't know, and want more.  Keep it going and do on.  BTW, I like the theme here on add and clone HTML element by using jQuery.  Pretty clean and professional.  Now I have to find some good info on how to start up and get this going in the right direction.  Right tool for the right job after all.

Hypercom T7Plus Manual

5/15/2010 8:04:19 AM #

Hypercom m4230

Seen any other sites other than add and clone HTML element by using jQuery?  It's like Hells Kitchen with Ramsey.  I mean he's with this cooks and merchants and they just dont even know how to run their businesses.  Ya know?

Hypercom m4230

5/16/2010 12:17:44 AM #

Computer Desks

I'm surfing the Internet searching for relevant information similar in your web logs. I'm impressed with the message. Thnx for the information, keep up the good work. I'll bookmark your web site so I can learn for updates after. Sorry for my poor english, it's non my first language

Computer Desks

5/19/2010 1:31:32 AM #

ceramic watch

This is good!

ceramic watch

5/19/2010 2:45:57 AM #

Hypercom m4240

I agree, and want more.  Keep it going and write on.  BTW, I like the theme here on add and clone HTML element by using jQuery.  Pretty clean and professional.  Now I have to find some good info on how to start up and get this going in the right direction.  Right tool for the right job after all.

Hypercom m4240

5/20/2010 1:58:03 AM #

Forest Sivils

I'm havin' some troubles trying 2 load your website bog. I've been read it many times before and never gotten something like this, but now when i try to load something it just takes a little while (4-15 minutes ) and then just stop. I've tried with "www." or not. Does anyone know what the problem could be? Please ask your support at hoster..And, yes, thanks a lot for your blog!

Forest Sivils

5/22/2010 5:54:41 AM #

ceramic watches

Good stuff, thanks for the post! Maybe you should do a follow up post about this?

ceramic watches

5/22/2010 5:40:57 PM #

Phone Detective Review

Hi, i must say fantastic site you have, i stumbled across it in AOL. Does you get much traffic?

Phone Detective Review

5/23/2010 3:35:26 AM #

free harry potter theme song

I am a huge lover music! Check my website, I am download songs here!
-----------------------------
Regards,
http://mp3ebs.com/

free harry potter theme song

5/23/2010 2:56:56 PM #

magnifying desk lamp

There are certainly a lot more details to take into consideration, but thanks for sharing this information

magnifying desk lamp

5/24/2010 1:23:29 AM #

lamp shades for table lamps

Well done dude.I love the animation.They are so cutely presented that i became a fan of yours'.

lamp shades for table lamps

5/24/2010 7:06:13 PM #

drugsmed.com online pharmacy reviews

Fanstastic ideas on your website. It's unusual these days to get websites with information you are interested in. I'm thankful I came upon your webpage.

drugsmed.com online pharmacy reviews

5/25/2010 2:56:39 PM #

Charlie

<a href="http://selkblog.com">youssef mzaoui</a> Interesting post :), take a look at mine <a href="http://selkblog.com">selkblog.com</a>

Charlie

5/28/2010 1:24:13 AM #

Devon Cothron

I'm a huge lover of your blog! Look at my website, You can submit your site there!

Devon Cothron

5/28/2010 3:09:04 AM #

Andrew Pelt

i cant beleive i stumbled onto your post..thanks so much!!! i am going to have to sign up 2 ur RSS feed so i can keep updated with your post...thanks Again

Andrew Pelt

5/29/2010 9:27:07 PM #

Waylon Eddy

Please, can you email me and tell me few more thinks about this, I am really fan of your blog...

Waylon Eddy

5/29/2010 9:28:10 PM #

Raymon Vassallo

You made some good points there. I did a search on the topic and found most people will agree with your blog.

Raymon Vassallo

5/29/2010 10:31:50 PM #

www.bulkdomainnews.com

I am a big lover of your weblog! Check out my website, You can analyze your site here!

www.bulkdomainnews.com

5/30/2010 12:47:02 AM #

Aubrey Askia

I usually don’t post in Blogs but your blog forced me to, amazing work.. beautiful …

Aubrey Askia

5/31/2010 1:26:35 AM #

www.proverkadomenov.ru

I am a big lover of your weblog! Look at my website, You can analyze your site there!

www.proverkadomenov.ru

6/4/2010 1:19:25 AM #

Hypercom M4230

Man, I've been looking all over for this stuff.  Thanks.  Do you have any other sites other than add and clone HTML element by using jQuery?  I'd like a list of the best way to prepage for all of this and what is the best place to start?  It'd be nice, no?

Hypercom M4230

6/7/2010 3:19:25 AM #

lose belly fat

It takes more thought to come up with a good blog comment then a bad one. For instance, it takes much less brain effort to say “you suck” than to say “I profoundly disagree with the simplistic drivel that has been touted on this networked opinion delivery system.” As evidenced by the number of moronic comments that are left on various blogs, it appears that the majority of blog readers have the cranial capacity of cattle who spend a lot of time defecating in the same pool they drink from.

lose belly fat

6/10/2010 6:38:38 AM #

Top Interview questions

Hrmmmmm.  add and clone HTML element by using jQuery seems a lot like   American Job Advocate.  I just wrote top 10 Interview Questions.  Just trying to spread the word. :) If you want to read it, the link is <a href="http://www.americanjobadvocate.com/content/top-10-job-interview-questions">http://www.americanjobadvocate.com/content/top-10-job-interview-questions</a>

Top Interview questions

6/14/2010 12:47:28 PM #

online casino

Good stuff, thanks for the post! Maybe you should do a follow up post about this?

online casino

6/14/2010 3:48:28 PM #

Lola

I  should be working instead of reading your cool blog about prepend.

Lola

6/15/2010 9:28:24 AM #

free website

Im not going to say what everyone else has already said, but I do want to comment on your knowledge of the topic. Youre truly well-informed. I cant believe how much of this I just wasnt aware of. Thank you for bringing more information to this topic for me. Im truly grateful and really impressed.

free website

6/17/2010 5:18:44 AM #

Beastiality Video

Im no expert, but I assume you just made an exceptionally nice place place. You naturally know what youre talking about, and I can basically get behind that. Thanks for getting so upfront and so truthful.

Beastiality Video

6/18/2010 10:03:19 AM #

Aleida Dreith

I was rather glad to find this website. I wanted to appreciate this outstanding read!! I most definitely relished every little bit  and I have you bookmarked to check out fresh new stuff you publish.

Aleida Dreith

6/21/2010 1:01:25 AM #

abercrombie

Very frequently I go to this site. It very much is satisfying to me. Thanks the author

abercrombie

6/24/2010 5:42:00 PM #

ebay sell

Nice, i like your articles a lot and will be excited to read more

ebay sell

6/26/2010 7:02:07 AM #

moshi monsters

http://wilbur97norton.edublogs.org/2010/06/24/freedom-from-harrassment-of-creditors-in-greenville-sc/

moshi monsters

7/3/2010 8:34:12 AM #

Fat Loss Tips

This blog contains really good stuff.Thanks for sharing this interesting blog.

Fat Loss Tips

7/8/2010 1:38:52 PM #

top ten job interview questions

I know you are using Microsofts BlogEngine.NET but can you please tell me which version that you are using?  I've been looking all over and wanted one like the one here at %BLOGSITE% I came from wordpress and am trying to figure out BlogEngine.

top ten job interview questions

7/9/2010 6:19:13 AM #

male enhancement

There's nothing better compared to performing at your peak. I love it, in addition to so does my girl.

male enhancement

7/27/2010 6:59:35 PM #

Burl Cheroki

I encounterd difficulties reading your weblog with safari browser, you would better update your weblog

Burl Cheroki

7/28/2010 5:03:21 AM #

magnetic name tags

Pretty good.  Just bookmarked it on digg

magnetic name tags

7/28/2010 7:26:07 AM #

Apple iPad

It's the little things that matter, that's what I believe.

Apple iPad

7/28/2010 11:23:05 AM #

Bags-Replica

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can turn this off?

Bags-Replica

7/28/2010 7:43:26 PM #

Lizabeth Heslep

Would you accept guest posts? I would love to put in writing couple articles here.

Lizabeth Heslep

7/28/2010 10:52:19 PM #

hypercom t7plus manual

Do you like running Blogengine?  If I might --perhaps you should consider adding a few videos.  I dont mean to disrespect what you've done; its very good stuff, for sure.  However, I think would respond to it more positively if you could add something animated to your posts.  Keep it up, but put a little more into it next time.

hypercom t7plus manual

7/29/2010 9:13:39 AM #

vrouw zoekt man

Hey how are you. I found your blog through Google and I just wanted to say that I think your writing is simply stunning! Thanks for providing this content for free.Nice information, kudos to you! It is good and correct. The usefulness and importance is overwhelming. Thanks again for this unbelievably powerful post and good luck!

vrouw zoekt man

7/30/2010 7:25:18 PM #

plakaty

Hi. I actually enjoyed reading your writing!. Top notch written content. I might recommend you to submit articles a bit more frequently. This way, having such type of a useful site I think you could rank better in the search engines

plakaty

8/8/2010 9:00:58 AM #

jillian michaels diet

I am surprised at the items I overlooked before I read this post. Thanks for the good information.

jillian michaels diet

8/10/2010 3:14:36 PM #

Boxelder Bugs

This blog has so much excellent information in it that i could spend all week reading it.

Boxelder Bugs

8/13/2010 9:40:07 PM #

fantasy rpg

Previews up to night out anime mmorpg's

fantasy rpg

8/18/2010 7:50:32 AM #

Top Twenty Hot Trends

It sounds like you're creating problems yourself by trying to solve this issue instead of looking at why their problem in the first place.

Top Twenty Hot Trends

8/18/2010 11:07:00 PM #

John

thanks for an interesting read :)

John

8/22/2010 6:25:56 PM #

Opony

Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.

Opony

8/25/2010 1:29:09 AM #

Edmond Vathroder

Good article. I just stumbled upon your blog and would like to let you know that I have really liked reading your blogs. Anyhow I'll be subscribing to your feed and I really hope you write again soon.

Edmond Vathroder

8/25/2010 12:55:45 PM #

used stationary bikes

great info thanks  

used stationary bikes

8/26/2010 5:12:05 PM #

Prada Shoes

when can we expect to see more blog post form you?? You have a captivating writing style so i can not wait for more.
http://www.noblechic.com/prada-mens.html

Prada Shoes

8/26/2010 6:31:26 PM #

Bags Replica

I have been looking for content like this for a research project I am working. Thanks very much. These types of cheap electronic gadgets used to be readily available down at the mall when stores like Brookstone and Sharper Image were around, but when those places closed down I obviously had to come up with a different way to feed my habit.

Bags Replica

8/29/2010 4:45:47 PM #

spokane siding contractor

spokane siding contractor

8/30/2010 1:01:22 AM #

north idaho siding and construction

north idaho siding and construction

9/1/2010 6:42:23 AM #

Xtrema cookware

amazing post ....lol

Xtrema cookware

9/1/2010 8:23:46 AM #

Cho Yung Tea

Thanks for the share, I have been reading online all day and this was actually worth reading. Thanks :)

Cho Yung Tea

9/2/2010 6:12:48 AM #

plastic name badges

Pretty good.  Just bookmarked it on digg

plastic name badges

9/2/2010 6:33:56 AM #

gold overmantle mirror

Comfortabl y, the article is really the top on this deserving topic. I suit in with your conclusions and will certainly thirstily look forward to your next updates. Just saying thanks will not just be sufficient, for the wonderful clarity in your writing. I can at once grab your rss feed to stay abreast of any kind of updates. Fabulous work and much success in your business enterprize!

gold overmantle mirror

9/2/2010 12:21:03 PM #

free websites for photographers

Interesting post :)

free websites for photographers

9/2/2010 11:57:30 PM #

french press

This website has a lot of really useful information on it! Thank you for helping me!

french press

9/3/2010 12:02:59 AM #

french press

Cool post ! Thanks for, commenting on my blog page mate! I will email you soon. I did not know that!

french press

9/5/2010 12:25:31 PM #

Olly

The search engines are wonderful, they deliver the info that you are searching for almost every time.They have delivered again this time.
Wonderful blog post thanks for sharing

Olly

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud