Please, I tried to create table containing just 3 cells. Tried to nest functions. Please, does someone see the mistake:
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Click the button to create a TABLE, a TR and a TD element.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
for(i=0; i<5; i<++)
{
function myFunction1{
var x = document.createElement("TABLE");
x.setAttribute("id", "myTable");
document.body.appendChild(x);
var y = document.createElement("TR");
y.setAttribute("id", "myTr");
document.getElementById("myTable").appendChild(y);
var z = document.createElement("TD");
var t = document.createTextNode("cell");
z.appendChild(t);
document.getElementById("myTr").appendChild(z)};
}
}
</script>
</body>
</html>
Put the script in the head of the HTML document,
put your code in a function, and create a call to the function in the ‘onload’ attribute of the body:
Body: <body onload="func();">
Script:
function func(){
tabElement=document.getElementById("ta");
t=tabElement.getElementsByTagsName("td");
t[0].style.backgroundColor="blue";
}
EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.
What an antiquated approach (that’s how it was done about two JavaScript versions back).
,Almost all JavaScript should be placed immediately before the </body> tag and inline event handlers are completely unnecessary - where you do need to process events (which isn’t necessary here) you use an event listener in the script itself.
It is also unnecessary to use the generic DOM calls on the individual table elements as there are special DOM calls specifically for tables that are far more efficient. See http://javascriptexample.net/domtable.php for a series of examples of all of the different table DOM calls including ones that can modify specific cells and even generating entire tables with only two createElement calls.
Is that to say that teaching document.write techniques to students today is the application of a teacher’s personal style?
Some around here would say that calls for a good ol’ fashioned hanging
Sometimes there’s a good reason to keep the old… Like when you’re building a template for e-mail… and you know what the say, history repeats itself and evertything comes back in fashion
Yes you can but the more antiquated approaches to JavaScript create a lot more work for the person maintaining the jumbled mess plus it eliminates the possibility of their using many of the new features that modern browsers provide.
For example all modern browsers support XHTML5 and XHTML does not allow document.write at all.
Also there is a security header available to disable any inline JavaScript so as to make injection almost impossible - you can’t use that security header if you jumble your JavaScript and HTML together (as is required for injection).