There are many ways to approach this, but since you complained (at least clearly in your original question original question) your code not seeming "less", here's something that illustrate why jQuery says "write less":
function MakeTablejQuery() {
var rows = $('#rows').val(), row;
var cols = $('#cols').val();
var table = $("<table>").attr("border", "1");
$('#placeholder').append(table);
for (var r = 0; r <= rows; r++) {
table.append(row = $("<tr>"));
for (var c = 0; c < cols; c++) {
row.append($(r ? "<td>" : "<th>").text(r ? c+""+r : "Column " + c));
}
}
}
Check a fiddle with both (plain JavaScript and jQuery) implementations.
Kinda nice, eh? When using jQuery to create elements, we have $('<tr>')
[1], which is an alternative to document.createElement('tr')
.
But that's not the biggest advantage here. The main difference is that, when you use append()
and text()
, jQuery returns the element in where you called those functions, whereas JavaScript's .appendChild()
returns the appended element (used as parameter). jQuery's approach allows a more fluent programming. (Also, just calling .text()
instead of having to call .createTextNode()
and .appendChild()
is so much sweeter.)
And, yeah, ok, I may have gone too far using the ternary operator, but, hey, isn't it great that even with it you can still read the code? If you only had .createTextNode()
, it wouldn't even be possible. But your question asks for less lines of code to show the power of jQuery not a plugin for table creation, right?
[1] You don't have to close the tag: $('<tr>')
works just as well as $('<tr/>')
or $('<tr></tr>')
. But don't take my word for it: check the docs.
There are many ways to approach this, but since you complained (at least clearly in your original question) your code not seeming "less", here's something that illustrate why jQuery says "write less":
function MakeTablejQuery() {
var rows = $('#rows').val(), row;
var cols = $('#cols').val();
var table = $("<table>").attr("border", "1");
$('#placeholder').append(table);
for (var r = 0; r <= rows; r++) {
table.append(row = $("<tr>"));
for (var c = 0; c < cols; c++) {
row.append($(r ? "<td>" : "<th>").text(r ? c+""+r : "Column " + c));
}
}
}
Check a fiddle with both (plain JavaScript and jQuery) implementations.
Kinda nice, eh? When using jQuery to create elements, we have $('<tr>')
[1], which is an alternative to document.createElement('tr')
.
But that's not the biggest advantage here. The main difference is that, when you use append()
and text()
, jQuery returns the element in where you called those functions, whereas JavaScript's .appendChild()
returns the appended element (used as parameter). jQuery's approach allows a more fluent programming. (Also, just calling .text()
instead of having to call .createTextNode()
and .appendChild()
is so much sweeter.)
And, yeah, ok, I may have gone too far using the ternary operator, but, hey, isn't it great that even with it you can still read the code? If you only had .createTextNode()
, it wouldn't even be possible. But your question asks for less lines of code to show the power of jQuery not a plugin for table creation, right?
[1] You don't have to close the tag: $('<tr>')
works just as well as $('<tr/>')
or $('<tr></tr>')
. But don't take my word for it: check the docs.
There are many ways to approach this, but since you complained (at least clearly in your original question) your code not seeming "less", here's something that illustrate why jQuery says "write less":
function MakeTablejQuery() {
var rows = $('#rows').val(), row;
var cols = $('#cols').val();
var table = $("<table>").attr("border", "1");
$('#placeholder').append(table);
for (var r = 0; r <= rows; r++) {
table.append(row = $("<tr>"));
for (var c = 0; c < cols; c++) {
row.append($(r ? "<td>" : "<th>").text(r ? c+""+r : "Column " + c));
}
}
}
Check a fiddle with both (plain JavaScript and jQuery) implementations.
Kinda nice, eh? When using jQuery to create elements, we have $('<tr>')
[1], which is an alternative to document.createElement('tr')
.
But that's not the biggest advantage here. The main difference is that, when you use append()
and text()
, jQuery returns the element in where you called those functions, whereas JavaScript's .appendChild()
returns the appended element (used as parameter). jQuery's approach allows a more fluent programming. (Also, just calling .text()
instead of having to call .createTextNode()
and .appendChild()
is so much sweeter.)
And, yeah, ok, I may have gone too far using the ternary operator, but, hey, isn't it great that even with it you can still read the code? If you only had .createTextNode()
, it wouldn't even be possible. But your question asks for less lines of code to show the power of jQuery not a plugin for table creation, right?
[1] You don't have to close the tag: $('<tr>')
works just as well as $('<tr/>')
or $('<tr></tr>')
. But don't take my word for it: check the docs.
There are many ways to approach this, but since you complained (at least clearly in your original question) your code not seeming "less", here's something that illustrate why jQuery says "write less":
function MakeTablejQuery() {
var rows = $('#rows').val(), row;
var cols = $('#cols').val();
var table = $("<table>").attr("border", "1");
$('#placeholder').append(table);
for (var r = 0; r <= rows; r++) {
table.append(row = $("<tr>"));
for (var c = 0; c < cols; c++) {
row.append($(r ? "<td>" : "<th>").text(r ? c+""+r : "Column " + c));
}
}
}
Check a fiddle with both (plain JavaScript and jQuery) implementations.
Kinda nice, eh? When using jQuery to create elements, we have $('<tr>')
[1], which is an alternative to document.createElement('tr')
.
But that's not the biggest advantage here. The main difference is that, when you use append()
and text()
, jQuery returns the element in where you called those functions, whereas JavaScript's .appendChild()
returns the appended element (used as parameter). jQuery's approach allows a more fluent programming. (Also, just calling .text()
instead of having to call .createTextNode()
and .appendChild()
is so much sweeter.)
And, yeah, ok, I may have gone too far using the ternary operator, but, hey, isn't it great that even with it you can still read the code? If you only had .createTextNode()
, it wouldn't even be possible. But your question asks for less lines of code to show the power of jQuery not a plugin for table creation, right?
[1] You don't have to close the tag: $('<tr>')
works just as well as $('<tr/>')
or $('<tr></tr>')
. But don't take my word for it: check the docs.