Im developing a web page and im using some javascripts on the pages to move some html elements, but i found that javascript code is not working when <!doctype html>
or any type of <!doctype>
s added to the top of the page ! what is this error?
I search on google and i found <!doctype html>
is the correct format even stackoverflow source code has it but when i used it javascripts not working but when it removed script working fine & html object is moving but my styles are gone away because of removing <!doctype html>
<!doctype html> when this line removed it works(html element is moving)
<html>
<head>
<script type="text/javascript">
function scrollDiv(){
var obj = document.getElementById("movingDiv");
obj.style.left = 100;
obj.style.top = 10;
}
document.onclick = scrollDiv;
</script>
</head>
<body class="body">
<div class="maindiv" id="mainDiv">
<div class="html_js_code" style="text-align:center;">
<div id="movingDiv" style="position:absolute;left:100pt;top:10pt;border:1px blue
outset;width:160px;background-color:lightyellow;font-weight:bold;">Moving Div</div>
</div>
</div>
</body>
</html>
Im developing a web page and im using some javascripts on the pages to move some html elements, but i found that javascript code is not working when <!doctype html>
or any type of <!doctype>
s added to the top of the page ! what is this error?
I search on google and i found <!doctype html>
is the correct format even stackoverflow source code has it but when i used it javascripts not working but when it removed script working fine & html object is moving but my styles are gone away because of removing <!doctype html>
<!doctype html> when this line removed it works(html element is moving)
<html>
<head>
<script type="text/javascript">
function scrollDiv(){
var obj = document.getElementById("movingDiv");
obj.style.left = 100;
obj.style.top = 10;
}
document.onclick = scrollDiv;
</script>
</head>
<body class="body">
<div class="maindiv" id="mainDiv">
<div class="html_js_code" style="text-align:center;">
<div id="movingDiv" style="position:absolute;left:100pt;top:10pt;border:1px blue
outset;width:160px;background-color:lightyellow;font-weight:bold;">Moving Div</div>
</div>
</div>
</body>
</html>
Share
Improve this question
edited May 31, 2016 at 6:55
Serenity
36.8k21 gold badges123 silver badges116 bronze badges
asked Jul 8, 2012 at 15:32
Naveen GamageNaveen Gamage
1,88412 gold badges33 silver badges52 bronze badges
1
- 1 Your logic, your way of thinking is good. Only the conclusion is wrong: the error must be in your Javascript code. – kapa Commented Jul 8, 2012 at 15:39
2 Answers
Reset to default 9Without a doctype, the browser assumes that unlabelled lengths are in pixels. With a doctype, they require an explicit unit.
obj.style.left = "100px";
obj.style.top = "10px";
you also forgot to put the parenthesis at the end of the function you called. I'm also pretty sure you passed it incorrectly. I believe it should look like this:
document.onclick = scrollDiv();
or
document.onclick(scrollDiv());