Initially the div id =" showHide" is invisible. I want to make it visible when the value of vall is not null. But the code is not working. Though variable vall is getting values, the div is still not visible. What is the problem with js codescript?? I am using jsp.
<head>
</head>
<body>
<div id =" showHide" style="display:none">
/*HTML TABLE*/
</div>
<script type="text/javascript">
alert("first alert");
var vall = "<%=vall%>";
if (vall != null)
{
var mydiv = getElementById.("showHide");
mydiv.style.display ="block";
alert("second alert");
<% System.out.println("jsvar="+vall);%>
}
</script>
</body>
When I logged in the console i got the error:
uncaught syntaxError: unexpected token (
and both the alert box are also not showing.
Initially the div id =" showHide" is invisible. I want to make it visible when the value of vall is not null. But the code is not working. Though variable vall is getting values, the div is still not visible. What is the problem with js codescript?? I am using jsp.
<head>
</head>
<body>
<div id =" showHide" style="display:none">
/*HTML TABLE*/
</div>
<script type="text/javascript">
alert("first alert");
var vall = "<%=vall%>";
if (vall != null)
{
var mydiv = getElementById.("showHide");
mydiv.style.display ="block";
alert("second alert");
<% System.out.println("jsvar="+vall);%>
}
</script>
</body>
When I logged in the console i got the error:
uncaught syntaxError: unexpected token (
and both the alert box are also not showing.
Share Improve this question edited Jun 20, 2012 at 5:28 sam asked Jun 20, 2012 at 4:15 samsam 4053 gold badges11 silver badges29 bronze badges 2 |7 Answers
Reset to default 7Try:
mydiv.style.display = "block";
instead of
mydiv.style.visibility = "visible";
visibility
and display
are different. Instead you should change the display
value from none
to block
(the default for a <div>
):
mydiv.style.display = "block";
Are you sure the content has actually been loaded before your JS is being called? Try moving your JS to the end of the body or wrap your code in a function that can be called on page load. If you want to be really fancy you should use jQuery to help you out with this. http://api.jquery.com/ready/
<div id =" showHide" style="display:none">
/*HTML TABLE*/
</div>
remove the space of id and put the Js code to back of div , try it.
I think You forget to declare variable mydiv.
replace this line mydiv = document.getElementById("showHide");
by
var mydiv = document.getElementById("showHide");
enter code here<div id ="showHide" style="display:none">
/*HTML TABLE*/
// javascript code just check this out
var vall = 10;
var mydiv = null
if (vall != null)
{
mydiv = document.getElementById("showHide");
mydiv.style.display ="block";
}
I think when your code in the script tag runs it does not find the div tag with that id, and when the div is rendered in the page you again set its visibility to hidden.
I suggest to put your script tag after the div.
instead of
var mydiv = getElementById.("showHide");
use
var mydiv = document.getElementById.("showHide");
document.getElementById("showHide");
to the incorrectgetElementById.("showHide");
. Change it back and the syntax error will go away. – Andrew Marshall Commented Jun 20, 2012 at 6:28