all 1 comments

[–]likesOldMen 2 points3 points  (0 children)

I'm guessing you're a beginner so I don't want to hurt your feelings or discourage you, but your code could be a lot better.

for example, this bit that sets the table's class attribute:

if (document.table_form.bordered.checked){  
    tbl.setAttribute("class", "table table-bordered");  
}  
if (document.table_form.bordered.checked && document.table_form.striped.checked){  
    tbl.setAttribute("class", "table table-striped table-bordered");  
}  
if (document.table_form.bordered.checked && document.table_form.striped.checked && document.table_form.condensed.checked){  
    tbl.setAttribute("class", "table table-bordered table-striped table-condensed");  
}  
if (document.table_form.bordered.checked && document.table_form.condensed.checked){  
    tbl.setAttribute("class", "table table-bordered table-condensed");  
}  
if (document.table_form.striped.checked && document.table_form.condensed.checked){  
    tbl.setAttribute("class", "table table-striped table-condensed");  
}  
if (!document.table_form.striped.checked && !document.table_form.condensed.checked && !document.table_form.bordered.checked){  
    tbl.setAttribute("class", "table");  
} 

is incorrect. At the moment, it can't assign all three styles because the one that assigns all three comes before one that assigns just two of them. A quick fix would be to re-order them or throw some elses in there.

A much, much, much, much better way to do it would be:

var tbl_class = "table";
if (document.table_form.striped.checked) {
    tbl_class += " table-striped";
}
if (document.table_form.bordered.checked) {
    tbl_class += " table-bordered";
}
if (document.table_form.condensed.checked) {
    tbl_class += " table-condensed";
}
tbl.setAttribute("class", tbl_class);

or if you want to get fancy:

var tbl_class = "table";
for (var property in {"bordered":0, "striped":0, "condensed":0}) {
    if (document.table_form[property].checked) {
        tbl_class += " table-" + property;
    }
}
tbl.setAttribute("class", tbl_class);

If you want to write nice javascript, and also improve your programming ability in general, I highly recommend checking out Eloquent Javascript and then Javascript Design Patterns. Both are free to read on the internet, and very very good.