Shoutbox

im n00b - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: im n00b (/showthread.php?tid=75191)

im n00b by Supersonicdarky on 06-08-2007 at 07:57 PM

why doesnt this change the width ;<

code:
<html>
<head>
<style type="text/css">
#a, #b, #c {
    height: 10px;
    border: 1px black solid;
    background: #00f;
    margin: 3px;
    display: block;
    }
</style>
</head>

<body>

<script>
document.getElementById('a').style.width="300px";
document.getElementById('b').style.width="400px";
document.getElementById('c').style.width="500px";
</script>

<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
</body>
</html>


* Supersonicdarky phails
RE: im n00b by Nathan on 06-08-2007 at 08:01 PM

not knowing much about javascript but:

document.getElementById('a').style.width="300px";

should be

document.getElementById('a').innerHTML = "width='300px';";


Im not sure if this will work:

But if I ever use GetElementById()

I always use " " insteas of ' '


RE: im n00b by Supersonicdarky on 06-08-2007 at 08:04 PM

doesnt work

" doesnt make a difference

head doesnt make a difference


RE: im n00b by Nathan on 06-08-2007 at 08:05 PM

Try putting the script in the head


RE: im n00b by WDZ on 06-08-2007 at 08:43 PM

It works for me if I move the <script> block below the divs, or put it in the <head> and execute it onload.


RE: im n00b by Supersonicdarky on 06-08-2007 at 09:03 PM

wtf, why doesnt it work when above divs? ^o)


RE: im n00b by Nathan on 06-08-2007 at 09:04 PM

try putting it in a function, then calling the funciton:

<script>width();</script>

or try putting in the body tag: onload="width();"

e.g: <body onload="alert('example, you could use your innerHTML stuff instead of this alert.')">

;)


RE: im n00b by Supersonicdarky on 06-08-2007 at 09:05 PM

it works, but i was wondering why only below


RE: im n00b by Plik on 06-08-2007 at 09:33 PM

quote:
Originally posted by Supersonicdarky
wtf, why doesnt it work when above divs? ^o)
Because the JS is excecuted when the parser gets to that node, at which point the DIVs don't exist, because the page hasn't loaded fully. Therefore when the JS is excecuted the DIVs can't be found, but when the script node is below the DIVs then they do exist.
You can have the JS above the divs by using onload like Nathen said.

quote:
Originally posted by Napbree
document.getElementById('a').style.width="300px";

should be

document.getElementById('a').innerHTML = "width='300px';";
nonononono way!
The first one is right, the second one will set the contents of the a div to "width='300px';", and wont change the width :P
innerHTML basically lets you access and change all the html within the node.
RE: im n00b by Felu on 06-09-2007 at 10:44 AM

It doesn't work because the script gets executed before the divs are created :rolleyes:.


RE: im n00b by markee on 06-09-2007 at 11:35 AM

and this is why you should put javascript in a function and execute it with an onload attribute of the body....