最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - show a div having style="display:none" after passing value to js variable - Stack Overflow

programmeradmin4浏览0评论

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
  • I have edited the code as per your suggestions but still it is not working. – sam Commented Jun 20, 2012 at 4:43
  • 1 That's because you changed your correct document.getElementById("showHide"); to the incorrect getElementById.("showHide");. Change it back and the syntax error will go away. – Andrew Marshall Commented Jun 20, 2012 at 6:28
Add a comment  | 

7 Answers 7

Reset to default 7

Try:

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");
发布评论

评论列表(0)

  1. 暂无评论