How can I make visible an invisible control with jquery? (hide and show not work)
Asked Answered
E

4

19

How can I change the visibility of a control with jQuery? I have a control that its visible property to false (not css).

When I used show() function for it nothing happened, it seems that hide() and show() methods are for css set of a control, not visible property.

Eisk answered 11/6, 2010 at 18:24 Comment(0)
U
40

You can't do this with jQuery, visible="false" in asp.net means the control isn't rendered into the page. If you want the control to go to the client, you need to do style="display: none;" so it's actually in the HTML, otherwise there's literally nothing for the client to show, since the element wasn't in the HTML your server sent.

If you remove the visible attribute and add the style attribute you can then use jQuery to show it, like this:

$("#elementID").show();

Old Answer (before patrick's catch)

To change visibility, you need to use .css(), like this:

$("#elem").css('visibility', 'visible');

Unless you need to have the element occupy page space though, use display: none; instead of visibility: hidden; in your CSS, then just do:

$("#elem").show();

The .show() and .hide() functions deal with display instead of visibility, like most of the jQuery functions :)

Undemonstrative answered 11/6, 2010 at 18:25 Comment(4)
The OP stated "(not css)". I can't imagine what that means, though.Radicand
@patrick - Ohhhhh, that's an ASP.Net thing, totally different answer, I'll update.Undemonstrative
that is very very bad for me ..... imagine that i have two buttons next together and when one of them is invisible (from codebehind)so the other one can fill it's space and this is exactly what i want.... i forced to invisible that button for some conditions and also forced to visible it with jquery and invisible the other one with jquery.... so with css -> hidden control i have white space ... how can i fill it by visible button??? however really really thanks for your attention and your answer// best regards/....Eisk
@LostLord - That's the part of the answer covering display, if you use display: none; instead of visibility: hidden;, then it won't occupy any space in the page while hidden.Undemonstrative
H
7

.show() and .hide() modify the css display rule. I think you want:

$(selector).css('visibility', 'hidden'); // Hide element
$(selector).css('visibility', 'visible'); // Show element
Heptangular answered 11/6, 2010 at 18:25 Comment(1)
that is very very bad for me ..... imagine that i have two buttons next together and when one of them is invisible (from codebehind)so the other one can fill it's space and this is exactly what i want.... i forced to invisible that button for some conditions and also forced to visible it with jquery and invisible the other one with jquery.... so with css -> hidden control i have white space ... how can i fill it by visible button??? however really really thanks for your attention and your answer// best regards/....Eisk
L
1

Here's some code I use to deal with this.

First we show the element, which will typically set the display type to "block" via .show() function, and then set the CSS rule to "visible":

jQuery( '.element' ).show().css( 'visibility', 'visible' );

Or, assuming that the class that is hiding the element is called hidden, such as in Twitter Bootstrap, toggleClass() can be useful:

jQuery( '.element' ).toggleClass( 'hidden' );

Lastly, if you want to chain functions, perhaps with fancy with a fading effect, you can do it like so:

jQuery( '.element' ).css( 'visibility', 'visible' ).fadeIn( 5000 );
Lota answered 9/6, 2012 at 8:1 Comment(0)
K
1

It's been more than 10 years and not sure if anyone still finding this question or answer relevant.

But a quick workaround is just to wrap the asp control within a html container

<div id="myElement" style="display: inline-block">
   <asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
</div>

Whenever the Javascript Event is triggered, if it needs to be an event by the asp control, just wrap the asp control around the div container.

<div id="testG">  
   <asp:Button ID="Button2" runat="server" CssClass="btn" Text="Activate" />
</div>

The jQuery Code is below:

$(document).ready(function () {
    $("#testG").click(function () {
                $("#myElement").css("display", "none");
     });
});
Kobylak answered 9/10, 2020 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.