Sunday 2 March 2014

Add dynamic Div using jQuery

Add dynamic Div using jQuery
Append at End of div
$($('.MyClass')[0]).parent().append($('<div>').text("Good Bye"));  
The above code will append "Good Bye" text div, to the class which is having name as: MyClass
You can search by Id also
Ex:$($('#MyDivID')[0]).parent().append($('<div>').text("Good Bye"));  
Above div will append in the End
Append at Start
How to append Div in the starting
Below code will add Welcome text in the starting of MyDivID div

$('<div id="newdiv">').text("Welcome").insertBefore($($('#MyDivID')[0]));
If you want to add image to div
$('#newdiv').prepend('<img" src="http://google.com/path/to/image.jpg" />');
Append at End of div
var newDiv = '<div><img src="img1.gif" alt="hi"/>Hi</div>';
$($('.myclass')[0]).append(newDiv);
Append at Start
var newDiv = '<div id="newDiv"><img src="img1.gif" alt="hi"/>Hi</div>';
$($('.myclass')[0]).before(newDiv );