Reputation: 21
Cannot understand why the onclick event gets invoked when i run the file when it should be invoked when i click on the div.
CODE:
<html>
<head>
<link rel="Stylesheet" type="text/css" href="shoppingcart.css">
</head>
<script type="text/javascript">
var count=0;
function swap()//cannot understand why it is running on loading the page
{
if(count==0)
{
document.getElementById('log').style.display='none';
count++;
}
else
{
document.getElementById('log').style.display='block';
count=0;
}
}
</script>
<body>
<div onclick="swap()" style="padding-left:30px;height:100px;width:300px;float:left;margin-right:180px" >
<h1 style="center">LOG IN</h1>
</div>
<div id="log" class="block border goudystout">
Email:<input type="email"/>
Password:<input type="password"/>
</div>
</body>
</html>
Upvotes: 0
Views: 54
Reputation: 774
The code works fine for me
It means you've an issue in your css file, maybe display:block!important; for .block class
<div id="log" class="block border goudystout">
That's breaking your code
Solve it!
Upvotes: 0
Reputation: 5283
For one, you've got the script section in-between the body and the head sections, it should be in the body section. This is probably the main reason it is not working as expected.
As per this article: http://www.simplehtmlguide.com/javascript.php...
It is legal to have the brackets in the call, you could try putting a semicolon after it, but javascript should be handling the automatic semi-colon insertion.
Upvotes: 0
Reputation: 731
Wild guess. You are setting swap()
as your div click handler and not swap
and hence its executing on page load.
Upvotes: 1