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 no comments on this article.
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.