1

How would I go about hiding the select element and whenever I click the button it would get displayed and when I click it again it would hide it? So far I have the following code It seems to not be working as I expected it to. I know toggle is probably the method in solving this problem in jquery.

<form id="myForm">
<input type='button' name='button' id='testbtn' value='Test Button' />
<br>
<select style="visibility:hidden" id='List' name='List'/>
</form>

$("#testbtn").click(function() {
   $('#List').toggle();
});
1
  • Lots of right answers thanks guys :) Commented Jun 21, 2011 at 21:21

5 Answers 5

4

Change:

<select style="visibility:hidden" id='List' name='List'/> </form>

To:

<select style="display:none" id='List' name='List'/> </form>

jQuery.toggle modifies the display CSS property

http://api.jquery.com/toggle/

Sign up to request clarification or add additional context in comments.

Comments

2

Change visibility:hidden to display:none. JQuery toggles the display attribute.

<form id="myForm">
<input type='button' name='button' id='testbtn' value='Test Button' />
<br>
<select style="display: none;" id='List' name='List'/>
</form>

$("#testbtn").click(function() {
   $('#List').toggle();
});

http://jsfiddle.net/apQYD/1/

Comments

0

Don't use visibility:hidden in your inline style. Use display:none instead.

Comments

0

as far as I know, toggle() relies on the display property, not the visibility one.

Try setting display:none instead of visibility:hidden.

<select style="display:none" id='List' name='List'/>

Comments

0

Here mention clearly that

The display property is saved and restored as needed. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

show i suggest another way to use class and remove style="" from select

css :

   .hide { display: none };

jQuery:

$("#testbtn").click(function() {
   $('#List').toggleClass('hide');
});

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.