PDA

View Full Version : Beginner DOM



pcbrainbuster
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 -

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

Please can anyone tell me whats wrong :)

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

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

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

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

Twey
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.

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

jscheuer1
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.

pcbrainbuster
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...

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

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

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

mburt
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)
x.removeChild(x.firstChild);

pcbrainbuster
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

mburt
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.

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