SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Switch Statement: Values 0,1 work - "Yes","No" don't..

    Showing/hiding a div ("#HIDEIFNO") based on which radio button is checked.
    If the possible values coming from teh buttons are "0" or '1", the below works great:

    Code:
    	$(function(){
    		$("#inc_tc_cov").change(function () {
    			var val = $(this).val();
    			switch(parseInt(val)){
    			case 1:
    				$("#HIDEIFNO").show();
    				break;
                            case 0:
    				$("#HIDEIFNO").hide();
    				break;
    			default:	
    			    $("#HIDEIFNO").hide();
    			}
    		});
    	});
    However, if I change the possible values to "Yes" or "No", I can't get it to work.
    Code should be the same, just "Yes" or "No" instead of 0 or 1, yes?

    Code:
    	$(function(){
    		$("#inc_tc_cov").change(function () {
    			var val = $(this).val();
    			switch(parseInt(val)){
    			case "Yes":
    				$("#HIDEIFNO").show();
    				break;
                           case "No":
    				$("#HIDEIFNO").hide();
    				break;
    			default:	
    			    $("#HIDEIFNO").hide();
    			}
    		});
    	});
    Case matches the radio button values.
    Tried single quotes as well.

    What does this poor idiot have wrong?

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,806
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    What is the value of the switch parameter parseInt(val)?

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    parseInt converts to base 10 so the result will always be a number and never "Yes" or "No"..

    You should never use parseInt without the second parameter anyway - the second parameter should be a number between 2 and 9 or 11 and 36 to indicate what base the supposed number being passed as the first parameter is in that you want to have converted to base 10.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    the second parameter should be a number between 2 and 9 or 11 and 36
    10 is feeling left out
    "First make it work. Then make it better."

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    10 is feeling left out
    Well it has its own Number() function and so doesn't need to share a function with the others.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    parseInt converts to base 10 so the result will always be a number and never "Yes" or "No.
    Duh "switch(parseInt(val))" - integer and all. Got that part.

    OK, if I wanted to check NOT integer values - back to "yes" or "no", or "pink" and "green" for that matter ... what would I need to change?

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by LesMizzell View Post
    Duh "switch(parseInt(val))" - integer and all. Got that part.

    OK, if I wanted to check NOT integer values - back to "yes" or "no", or "pink" and "green" for that matter ... what would I need to change?
    Just remove the parseInt()
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •