Code in index.html
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
<body onload="loadIndex()">
<a id="registration" href="register.html"> Register </a>
</body>
Code in index.js
function loadIndex() {
alert("Page is loaded");
$("#registration").hide();
}
I get alert, but link is not hidden. I have more files, and this does not work in any of them. I think that mistake is not in code, but I don't know where is.
Your code is fine with the exception of a tiny syntax issue. You're just missing the closing tag </head>
. Check code below:
function loadIndex() {
alert("Page is loaded");
$("#registration").hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
<body onload="loadIndex()">
<a id="registration" href="register.html"> Register </a>
</body>
</div>
Try this setup:
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script>
$(document).ready(function(){
alert("Page is loaded");
$("#registration").hide();
});
</script>
</head>
<body>
<a id="registration" href="register.html"> Register </a>
</body>
I had same problem with jQuery v1.11
before, I solved this problem by creating new class in my css file as below :
.hidden {
display : none;
}
and for show and hide elem, I added and removed hidden class from elem.
hide: $(elem).addClass('hidden');
show: $(elem).removeClass('hidden');