Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    show/hide on index id

    Hello

    I have the code below and I am trying to add another index id to be checked to the two that I already have. I have tried changing the line:

    var anotherTab = (indx.indexOf('a') >= 0) ? id + 'b' : id + 'a';

    to

    var anotherTab = (indx.indexOf('a') >= 0) ? id + 'b' : id + 'a' : id + 'c';

    but I just get a javascript error what would be the correct syntax for a third index id?




    Code:
            var groupId=obj.id.substring(0, 1);
    	var indx=obj.id.substring(obj.id.indexOf('_')+1);
    	var id=indx.substring(0, indx.length-1);
    
    	var anotherTab = (indx.indexOf('a') >= 0) ? id + 'b' : id + 'a';
    	
    	document.getElementById(groupId+'tab_' + indx).className = 'selected';
    	document.getElementById(groupId+'tab_' + anotherTab).className = '';	
    	
    	if (indx==id+'a')
    	{
    		show (groupId+'baseballInfo_'+id);
    		hide (groupId+'footballInfo_'+id);
    	        hide (groupId+'soccerInfo_'+id);
    	}
    	else if (if (indx==id+'a')
    	{
    		hide (groupId+'baseballInfo_'+id);
    		show (groupId+'footballInfo_'+id);
    	        hide (groupId+'soccerInfo_'+id);
    	}
       else
           {
                    hide(groupId+'baseballInfo_'+id);
    		hide (groupId+'footballInfo_'+id);
    	       show(groupId+'soccerInfo_'+id);
    
    	
    		
    }

  • #2
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Your bent on nested ternary operators? It would be something like:

    var $value = ( indx.indexOf('a') >= 0 ) ? id + 'b' : (indx.indexOf('a') <= 0) ? id + 'a' : id + 'c';

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,148
    Thanks
    39
    Thanked 506 Times in 500 Posts

    Lightbulb

    This is just a guess as to what you are trying to accomplish.

    Instead of: var anotherTab = (indx.indexOf('a') >= 0) ? id + 'b' : id + 'a';
    Try:
    Code:
    var anotherTab = id;
    var indx = 'abc'.indexOf(id);
    switch (indx) {
      case 0 : anotherTab += 'a';  break;
      case 1 : anotherTab += 'b';  break;
      case 2 : anotherTab += 'c';  break;
    // add more if needed ...
      default : alert('Invalid ID');  break;
    }
    Code is untested because I don't have access to the initializing variables.


  •  

    Posting Permissions

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