Udders Udders - 1 month ago 8
jQuery Question

Javascript creating <div> on the fly

I have a a link that looks similar to this

<a href="/home/category/blog/1" id="blog">Blog</a>


As you can the link has an ID of 'blog' what I want to do is to create an div on the fly with the ID from the link that was clicked so if the 'blog' is clicked, then the markup would be

<div id="blog">
<!--some content here-->
</div>


Like wise if for instance the news link is clicked then I would like,

<div id="news">
<!--some content here-->
</div>


to be created in the markup if this possible? and how Im pretty new to jQuery.

Answer

Try this:

$("a").click(function(){
    $("#wrapper").append("<div id=" + this.id + "></div>");
});

Not tested, should work ;) where: #wrapper is parent element, work on all a as you see.