Dynamically create elements with jQuery
Written by: maffelu , 2009-12-21 13:28:03
<html>
<body>
</body>
</html>
<html>
<body>
<div>
</div>
</body>
</html>
<html>
<head>
<!--Load the jquery lib -->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<!--Use jquery -->
<script type="text/javascript">
$(document).ready(function(){
$('#myLink').click(function(){
var newParagraph = $('<p />').text("Goodbye world...");
$('#myDiv').append(newParagraph);
});
});
</script>
</head>
<body>
<div id="myDiv">
</div>
<a href="#" id="myLink">Change text</a>
</body>
</html>
<html>
<head>
<!--Load the jquery lib -->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<!--Use jquery -->
<script type="text/javascript">
$(document).ready(function(){
$('#myLink').click(function(){
/*
var newParagraph = $('<p />').text("Goodbye world...");
$('#myDiv').append(newParagraph);
*/
$('<p />').text("Goodbye world...").appendTo($('#myDiv'));
});
});
</script>
</head>
<body>
<div id="myDiv">
</div>
<a href="#" id="myLink">Change text</a>
</body>
</html>
<html>
<head>
<!--Load the jquery lib -->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<!--Use jquery -->
<script type="text/javascript">
$(document).ready(function(){
var nextUrlId = 2;
$('#AddUrl').click(function(){
//Create and add a paragraph
$('<p />').attr('id', 'urlParagraph' + nextUrlId)
.text("URL: ")
.appendTo('#inputBoxes');
//Create and add an input box
$('<input />').attr({'type':'text', 'id':'url' + nextUrlId})
.appendTo('#urlParagraph' + nextUrlId);
//Iterate id number
nextUrlId++;
});
});
</script>
</head>
<body>
<form id="myForm" action="somePage.php" method="get">
<div id="inputBoxes">
<p id="nameParagraph">Name: <input type="text" id="name" /></p>
<p id="urlParagraph1">URL: <input type="text" id="url1" /></p>
</div>
<p><input type="submit" id="submit" /> or <a href="#" id="AddUrl">Add another URL</a></p>
</form>
</body>
</html>
<html>
<head>
<!--Load the jquery lib -->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<!--Use jquery -->
<script type="text/javascript">
$(document).ready(function(){
var rowNumber = 0;
$('#AddResults').click(function(){
//Get the user input
var nameInput = $('#nameInput').val();
var urlInput = $('#urlInput').val();
//Create a new row with an ID
var newRow = $('<tr />').attr('id', 'row' + rowNumber);
//Add some HTML to the row
newRow.html('<td>' + nameInput + '</td><td>' + urlInput + '</td>');
//Append the new row to the body of the #myTable table
$('#myTable tbody').append(newRow);
//Iterate row number
rowNumber++;
});
});
</script>
</head>
<body>
<table id="myTable">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p>Name: <input type="text" id="nameInput" /></p>
<p>URL: <input type="text" id="urlInput" /></p>
<p><a href="#" id="AddResults">Add</a></p>
</body>
</html>
<html>
<head>
<!--Load the jquery lib -->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<!--Use jquery -->
<script type="text/javascript">
$(document).ready(function(){
$('#myLink').click(function(){
$('<p />').text("Goodbye world...").appendTo($('#myDiv'));
});
$('p').click(function(){
alert("This paragraph has the following text:\n" + $(this).text());
});
});
</script>
</head>
<body>
<div id="myDiv">
<p>Hello world!</p>
</div>
<a href="#" id="myLink">Add new paragraph</a>
</body>
</html>
<html>
<head>
<!--Load the jquery lib -->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<!--Use jquery -->
<script type="text/javascript">
$(document).ready(function(){
$('#myLink').click(function(){
$('<p />').text("Goodbye world...").appendTo($('#myDiv'));
});
$('p').live("click", function(){
alert("This paragraph has the following text:\n" + $(this).text());
});
});
</script>
</head>
<body>
<div id="myDiv">
<p>Hello world!</p>
</div>
<a href="#" id="myLink">Change text</a>
</body>
</html>
There are 14 comments on this article.
Anik
2011-01-17 04:14:18
how to do pagination using jquery?
sunny
2011-01-26 16:03:13
this tutorial is awesome! exactly what i was looking for.. i was running into the problem where my clicks werent working but now i used the .live() and it works now!
Maffelu
2011-01-27 12:59:58
Sunny, remember to not over use the .live() event since it binds the events straight to the document making them quite resource hogging.
I'm glad it helped you!
deepa
2011-06-09 03:49:27
Nice tutorial
parmjeet
2011-07-18 02:17:05
Good examples..
Specialist
2011-09-02 13:17:34
Good Exemple, helpme a lot
chan
2011-11-09 09:46:24
this is a great article......i was finding a solution to this problem for a one week....u solved it...sorry for my english...
great article...thanx..
Faheem Shah
2012-01-11 01:59:34
Nice article. This is very helpful for me...
david
2012-01-12 23:24:17
I like very much
Yadav
2012-01-19 03:09:55
I am facing a problem when I create a new div dynamically and popup on it, its working but when closing the window I remove the div and refresh the page.
Problem is the page is not refreshing and it going hang out
Jibin Joy
2012-01-31 10:18:47
Nice Example...
Thanks
John
2012-02-02 00:12:19
Nice One..........
prakash
2012-02-06 10:26:19
nice...one.i have faced same proble.and i searched on google..finally i got it
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
Ashish
2010-12-22 23:36:28
thnx dude, got exactly very good example for create DOM element with help of jquery, thnx alot