Results 1 to 3 of 3

Thread: Making a div appear and disappear

  1. #1
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Making a div appear and disappear

    Good morning,

    I'm trying to make a div appear/ disappear based on which radio button a user clicks. Initially the div is hidden, when the user clicks "Yes" the div correctly appears but when the user clicks "No" it remains visible. I added a couple of alerts and it seems that the radio button value is always seen as "Yes".

    What I'm trying to do is when the user answers "yes" to the first question, then the second question should appear, but if they change their mind and then click "No" the second question should disappear.

    Here's part of the page:

    Code:
     <p>Will you be selling Products and/ or services during the rally?</p>
      <input type="radio" id="selly" name="sell" value="yes" /> <label for="selly">Yes</label><br />
    <input type="radio" id="selln" name="sell" value="no" /> <label for="selln">No</label>
    
     <div id="haveBl" hidden="true">    
     <p>Do you already have a City of Hollister Business License (valid during the rally)?</p>
       <p>
         <label>
           <input type="radio" name="havebl" value="Yes" id="havebly" />
           Yes</label>
         <br />
         <label>
           <input type="radio" name="havebl" value="No" id="havebln" />
           No</label>
         <br />
       </p>
       </div> <!-- /haveBl -->
    and here's the js

    Code:
     <script>
      $('input[name=sell]').click(function() {
        if (sell = "yes") 
    	{
    	 $('#haveBl').show();
    	 alert ("selly = " + sell);
    	}
    	else 
    	{
    		$('#haveBl').hide();
    		alert ("selln = " + sell);
    	};
    });
      </script>
    The full page can be seen here http://www.hollisterairshow.com/rally

    Thanks for any direction you can give. The learning curve on js seems quite steep - at least where I am.....

    Tony

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script>
      $('input[name=sell]').click(function() {
        if (this.value === "yes") 
    	{
    	 $('#haveBl').show();
    	 alert ("selly = " + this.value);
    	}
    	else 
    	{
    		$('#haveBl').hide();
    		alert ("selln = " + this.value);
    	};
    });
    </script>
    Of course, you don't need those alerts then. There are various ways. If you want to get real cute, you can use the value as the name of the action to take:

    Code:
        <p>Will you be selling Products and/ or services during the rally?</p>
      <input type="radio" id="selly" name="sell" value="show"> <label for="selly">Yes</label><br>
    <input type="radio" id="selln" name="sell" value="hide"> <label for="selln">No</label>
    
    <script>
      $('input[name=sell]').click(function() {
    	$('#haveBl')[this.value]();
    });
    </script>
    But if the input is submitted later as part of a form or AJAX call (neither of which it looks like right now, but I'm not sure), the sell name will have that (show or hide) value. But that might not be a bad thing.
    Last edited by jscheuer1; 01-27-2014 at 06:49 PM. Reason: add 'cute' method
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Hi John,

    Thank you again, that worked perfectly. I had thought about using the "cute" approach but couldn't figure out how to do it. I'm sure I'll use that in the future.

    Regards,

    Tony

Similar Threads

  1. Appear/Disappear Problems
    By Clayf700 in forum JavaScript
    Replies: 2
    Last Post: 01-01-2011, 09:42 PM
  2. submenus disappear
    By mcolton in forum Dynamic Drive scripts help
    Replies: 7
    Last Post: 07-17-2009, 11:37 AM
  3. Making a DIV appear then disappear?
    By Remotive in forum JavaScript
    Replies: 1
    Last Post: 08-26-2007, 06:16 PM
  4. image disappear when clicking ?
    By tzvio in forum JavaScript
    Replies: 1
    Last Post: 10-30-2006, 10:58 PM
  5. mouseover buttons disappear
    By java joe in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 07-24-2005, 11:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •