Need a little help at JS. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need a little help at JS.

In my this code... <style> #ChandlerBing { background-color:red; width:456px; height:456px; }</style> <script> var JoeyTribbiani=document.getElementById ("ChandlerBing"); JoeyTribbiani.style.height=1px;</script> Why my code is not working and how can I correct it? Thanks for answering.

2nd Oct 2017, 4:11 AM
Isair Calhawk
Isair Calhawk - avatar
9 Answers
+ 2
The only possible reason i can think of is that sololearn platform runs the JS first before the HTML is loaded, so it is detecting the style being null. So I suggest the following instead: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="Bing">hjw</div> </body> <script> var Bong=document.getElementById("Bing"); Bong.style.height="1px"; </script> </html> By putting the script at the bottom (instead of under JS tab), this ensures that the script is run after the HTML is loaded
2nd Oct 2017, 6:04 AM
Deddy Tandean
+ 1
U can try this with quotation mark: JoeyTribbiani.style.height = "1px"; It should work :)
2nd Oct 2017, 4:31 AM
Deddy Tandean
+ 1
what error did u experience? The result shd be showing you 1px height instead of 456px right away
2nd Oct 2017, 4:40 AM
Deddy Tandean
+ 1
Don't ask me why, but I changed the double quotations to single quotations and I think that I got it to work. Jo.style.height='1px';
2nd Oct 2017, 5:07 AM
Jeff Shipton
Jeff Shipton - avatar
2nd Oct 2017, 5:08 AM
Jeff Shipton
Jeff Shipton - avatar
0
I have tried it, still an error
2nd Oct 2017, 4:32 AM
Isair Calhawk
Isair Calhawk - avatar
0
I have tried it, still an error
2nd Oct 2017, 4:32 AM
Isair Calhawk
Isair Calhawk - avatar
0
I have tried it, still an error
2nd Oct 2017, 4:32 AM
Isair Calhawk
Isair Calhawk - avatar
2nd Oct 2017, 5:55 AM
Isair Calhawk
Isair Calhawk - avatar