I am getting this javascript error: "block is not defined"
<script type="text/javascript">
$(document).ready(function()
{
$(".register_now").click(function()
{
$(".fp_top_right_login").slideToggle(600);
var st = document.getElementById("fp_top_right_register").style.display;
if(st == "" || st == "none")
{
window.setTimeout(document.getElementById("fp_top_right_register").style.display="block",600); //error happens here
}
else
{
window.setTimeout(document.getElementById("fp_top_right_register").style.display="none",600); //and also here
}
});
});
</script>
I am getting this javascript error: "block is not defined"
<script type="text/javascript">
$(document).ready(function()
{
$(".register_now").click(function()
{
$(".fp_top_right_login").slideToggle(600);
var st = document.getElementById("fp_top_right_register").style.display;
if(st == "" || st == "none")
{
window.setTimeout(document.getElementById("fp_top_right_register").style.display="block",600); //error happens here
}
else
{
window.setTimeout(document.getElementById("fp_top_right_register").style.display="none",600); //and also here
}
});
});
</script>
Share
Improve this question
asked Sep 15, 2011 at 13:49
AllisonCAllisonC
3,1004 gold badges31 silver badges49 bronze badges
4
- 1 Can you make a JsFiddle with a testcase of the problem? – Exelian Commented Sep 15, 2011 at 13:51
-
So no one else has yet asked why you're using "getElementById" right next to a jQuery selector? Use
$('#foo')
instead! – Adam Terlson Commented Sep 15, 2011 at 14:10 -
Why are you using
document.getElementById("fp_top_right_register").style.display="block"
when you're using jQuery anyway?$("#fp_top_right_register").show()
isn't any faster, but it's a lot shorter and easier to read. – Blazemonger Commented Sep 15, 2011 at 14:14 - This code wasn't written by me, I'm just fixing things on our site – AllisonC Commented Sep 15, 2011 at 15:07
3 Answers
Reset to default 7setTimeout
takes a function as a parameter. You can use an anonymous function. Example:
window.setTimeout(function() {
document.getElementById("fp_top_right_register").style.display="block";
}, 600);
There is an error in you way of using setTimeout.
window.setTimeout(function(){document.getElementById("fp_top_right_register").style.display="block"},600);
Also, consider using jquery css :
$('#fp_top_right_register').css('display','block');
The first argument to window.setTimeout
should be a function, not a string, which is the result of your assignment.
You probably wanted to wrap that assignment in
function () { .... }