View Full Version : Beginner DOM

02-26-2007, 10:25 PM
Hello :),

I just started learning what DOM is - though it is ssimilar to HTML it hasthat parent uncle...

And in one of my tests i did something wrong and here it is -

<div align="center" id="maindiv">Hello I want fruit for today, OK ?</div>
<button onclick="asd()">Remove The DIV</button>
var x=document.getElementById("maindiv");
function asd() {

Please can anyone tell me whats wrong :)

02-26-2007, 10:34 PM
arent you supposed to put a </script> ? or does that not matter

02-26-2007, 10:38 PM
Lol what a typo :)

Thats correct ma lad i forgot to put the script tag (end) in :)

02-26-2007, 10:39 PM
your welcome :P happy to help

02-26-2007, 11:30 PM
Like I said, if your pages don't validate (http://validator.w3.org/), there's no guarantee you'll be able to use DOM methods.

02-27-2007, 02:06 AM
you shouldn't put your scripts in the body plus try to add the scripts language and type.

02-27-2007, 05:56 AM
you shouldn't put your scripts in the body plus try to add the scripts language and type.

Forget language, its deprecated. Type is all that is required for valid script tags. The language attribute will not validate but, it can be useful for hacks if you want to go down that slippery slope.

02-27-2007, 07:40 AM
Grrrrr, i never liked dumb sorry dom in the first place, document.getElementById was just fine and so was innerHTML...

02-27-2007, 07:44 AM
Whats not valid about it though APART from the type part - i read that was not required

02-27-2007, 10:13 PM
Were did every one run off too !!?? :confused:

02-27-2007, 11:49 PM
You have to understand that putting the <head> in the <body> IS invalid.

02-27-2007, 11:58 PM
Also, x is not a parent node of document.getElementById("maindiv"). If you want to remove it's content:

while (x.firstChild)

02-28-2007, 07:32 AM
I did not put the head in the body :)
And like the title suggests its pretty much begginner to dom -
can't you use if instead of while

02-28-2007, 11:19 AM
No, because if will only run once if the query returns true (or whatever the result is). While loop will keep repeating the process until the statement is false. firstChild is basically the first element, or element text in a node.

02-28-2007, 04:27 PM
OK I see :)