SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nested if-else in a function

    Could someone tell me the proper way to write the following big-*** "if" statement into the following function (I did the "if" stuff in english not JS):


    function getBullet (bul_level) {

    if bul_level=0 THEN bulSrc= bulRoot.png

    ELSE

    if bul_level =1 THEN (((if <current display id style>="block" then bulSrc=bulMinusA.png ELSE if <current display id style>="block"> then bulSrc=bulPlusA.png)))

    ELSE

    if bul_level =2 THEN (((if <current display id style>="block" then bulSrc=bulMinusB.png ELSE if <current display id style>="block"> then bulSrc=bulPlusB.png)))


    ELSE

    if bul_level=4 THEN bulSrc= bulRoot.png

    }



    Regarding the "<current display id style>="block"" and "none", I'm trying to say SOMETHING LIKE (from different code, but the right idea):

    if ( thisLevel.style.display == "none") {
    thisLevel.style.display = "block";
    thisImg.src = bulMinus_tree0.src;
    }
    else {
    hideLevel( _levelId, _imgId);
    }
    }

    if ( thisLevel.style.display == "none") {
    thisLevel.style.display = "block";
    thisImg.src = bulMinus_tree0.src;



    if someone says ...onClick="getBullet(0)" it should set the bulSrc to bulRoot.png

    if someone says ...onClick="getBullet(1)" it should set the bulSrc to bulMinusA.png if the display="block" and bulPlusA.png if the display="none"

    if someone says ...onClick="getBullet(2)" it should set the bulSrc to bulMinusB.png if the display="block" and bulPlusB.png if the display="none

    if someone says ...onClick="getBullet(4)" it should set the bulSrc to bulLink.png


    an example of the code I'm using to get this function going is:

    <a href="#" onClick="getBullet(X);return false"><img src="./images/bulStart.png"

    any help very much appreciated

    shawn
    Last edited by shawn gibson; Mar 16, 2002 at 18:34.

  2. #2
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This doesn't work, but am I close?

    function getBullet (bul_level) {
    call subLevel (); //get value for getLevel
    if (var bul_level=0) {bulSrc="bulRoot.png"};
    if (var bul_level=1) & (getLevel=0) {bulSrc="bulMinusA.png"};
    if (var bul_level=1) & (getLevel=1) {bulSrc="bulPlusA.png"};
    if (var bul_level=2) & (getLevel=0) {bulSrc="bulMinusB.png"};
    if (var bul_level=2) & (getLevel=1) {bulSrc="bulPlusB.png"};
    if (var bul_level=3) {bulSrc="bulLink.png"}
    }



    function subLevel (_levelId)
    var thisLevel = document.getElementById( _levelId );
    if ( thisLevel.style.display == "block") {
    new var getLevel=0);
    }
    if ( thisLevel.style.display == "none") {
    new var getLevel=1);
    }
    Last edited by shawn gibson; Mar 16, 2002 at 22:22.

  3. #3
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    yet again not read

    Sorry, don't have time to read the whole thing but:

    if (){ }
    else if () {}
    else if () {}
    else {}

    is the logic bundle structure for Javascript.


    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  4. #4
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "switch" statement makes multiple IF checks for the same variable redundant. Consult your favourite documentation! I tried to paste the relevant MS documentation here, but HTML is disabled!

    Try this (untested, based on your posted code):
    Code:
    function getBullet(bul_level)
    { 
    	var getLevel = subLevel(); //get value for getLevel
    	
    	switch(getLevel)
    	{
    		case 0:
    			bulSrc="bulRoot.png"; 
    			break;
    			
    		case 1:
    			if(getLevel=0) {bulSrc="bulMinusA.png";}
    			else {bulSrc="bulPlusA.png";}
    			break;
    			
    		case 2:
    			if(getLevel=0) {bulSrc="bulMinusB.png";}
    			else {bulSrc="bulPlusB.png";}
    			break;
    		
    		case 3:
    			bulSrc="bulLink.png";
    			break;
    	} 
    }
    
    
    function subLevel(_levelId)
    { 
    	var thisLevel = document.getElementById(_levelId);
    	
    	switch(thisLevel)
    	{
    		case "block":
    			return 0;
    			break;
    		
    		case "none":
    			return 1;
    			break;
    	}
    }

    M@rco
    Last edited by M@rco; Mar 18, 2002 at 11:40.


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
  •