Home > The joys of jQuery

The joys of jQuery

July 16, 2009 Mike 0 comments
After some experimentation today with various jQuery features, I noticed that in a "single sentence" it was possible to rapidly build up the contents of a table row. That's nothing particularly profound in its own right, but it's an example of why I like jQuery so much:

<html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        table = $("<tbody/>");
        thead = $("<thead/>");
        theadtr = $("<tr/>");              
       
        $("<th/>").text("Field 1").appendTo(theadtr)
        .clone().text("Field 2").appendTo(theadtr);
       
        theadtr.appendTo(thead);
        thead.appendTo(table);
        table.appendTo($("#container"));
    });
    </script>
</head>
<body>
    <div id="container"></div>
</body>
</html>
Categories: Tags: , ,
  1. No comments yet.