jQuery - Add a row to a table


Recommended Posts

Hey,

I posted this tutorial on my blog. It describes the use of jQuery and how to add a row to a table. Hopefully this will be one in many tutorials.

Feedback would be good, I did this in 10 minutes I had free whilst working. Helped me concentrate again.

http://www.james-brooks.net/?p=281

James

Link to comment
https://www.neowin.net/forum/topic/792200-jquery-add-a-row-to-a-table/
Share on other sites

When hooking into anchor tags, you should be returning false to stop the default action of navigating to the location in the href, even if it's only a #.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Add Row example</title>
<script src="dist/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
 <!-- jQuery Code will go underneath this -->
 $(document).ready(function () {	
 	// Code between here will only run when the document is ready
 	$("a[name=addRow]").click(function() {
 		// Code between here will only run when the a link is clicked and has a name of addRow
 		$("table#myTable tr:last").after('<tr><td>Row 4</td></tr>');
		return false;
 	});
 });
</script>
</head>

<body>

<table style="width: 100%" id="myTable">
 <tr><td>Row 1</td></tr>
 <tr><td>Row 2</td></tr>
 <tr><td>Row 3</td></tr>
</table>
<a href="#" name="addRow">Add Row</a>

</body>

</html>

When hooking into anchor tags, you should be returning false to stop the default action of navigating to the location in the href, even if it's only a #.

Yes you are correct. This is what happens when you don't have that much time.

I shall add it when my network is working again.

  • 1 year later...

Seems like your page has been moved.. Can you please update the link?

Vebj?rn

Hey,

I posted this tutorial on my blog. It describes the use of jQuery and how to add a row to a table. Hopefully this will be one in many tutorials.

Feedback would be good, I did this in 10 minutes I had free whilst working. Helped me concentrate again.

http://www.james-brooks.net/?p=281

James

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.