1

I have two conditions, I want to hide and show a div with javascript or with c# code. Using runat="server", I am able to hide the div with C#. Is it possible to hide the div with javascript? I tried the following:

<div id="divpassword" runat="server" style="display: none;" >
----
----
</div>

document.getElementById('<%= divpassword.ClientID %>').style.display = 'block';
Marv
  • 748
  • 11
  • 27
amit
  • 429
  • 5
  • 8
  • 20
  • Try adding `clientidmode="static"` to your div – User2012384 Jan 12 '16 at 08:19
  • Your code looks fine to me, I've used this method many times. Try looking at the source code in your browser to see what `<%= divpassword.ClientID %>` actually puts in the call to `getElementById`, if that looks OK then use the JavaScript console to query what `document.getElementById('copy the id from view source')` is, and then add `.style.display` to see what the display tag is. You may find you have a typo, or something simple which is causing this to fail. – Vitani Jan 12 '16 at 08:54

3 Answers3

2

You can use runat="server" and also hide the div from javascript using clientidmode="static".

<div id="divpassword" runat="server" clientidmode="static" style="display: none;" >
----
----
</div>

document.getElementById('divpassword').style.display = 'block';
Rubysmith
  • 1,165
  • 8
  • 12
0

It can be very easily done if you use jQuery.

$( "#divpassword" ).hide();

And if you need to do it in pure java script, the following link might help - Show/hide 'div' using JavaScript

Community
  • 1
  • 1
Yogi
  • 9,174
  • 2
  • 46
  • 61
  • @JᴀʏMᴇᴇ: Appriciate, if you can make me more clear on what was unnecessary, was't the answer is solving the problem?. Is there's anything I interpreted incorrect? – Yogi Jan 12 '16 at 09:08
  • @JᴀʏMᴇᴇ: Thanks for your reply. That is why I have added link for using JS. As it is not clear if OP is using jQuey or not, even if not, this gives an additional facet to it. Thanks :) – Yogi Jan 12 '16 at 09:13
0

You can go many ways.

Way #1:

<div id="divpassword" runat="server" style="display: none;" >
----
----
</div>
<script language="javascript">document.getElementById('<%= divpassword.ClientID %>').style.display = 'block';</script>

Way #2:

<div id="divpassword" runat="server" clientidmode="static" style="display: none;" >
----
----
</div>
<script language="javascript">document.getElementById('divpassword').style.display = 'block';</script>
procma
  • 1,174
  • 3
  • 14
  • 24