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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts

    quick bool check problem.

    I thought this would have worked but it doesn't seem to. Not very familiar with java script anymore I guess. Someone see what I am doing wrong?

    Code:
    <style type="text/javascript">
    var contact = document.getElementById("contactPage") ? true:false;
    var projects = document.getElementById("productPage") ? true:false;
    var about = document.getElementById("aboutPage") ? true:false;
    var home = document.getElementById("homePage") ? true:false;
    
    var checkPage = function (contact, projects, about, home)
    { 
    	if (contact === true)
    	else(projects === true)
    	else(about === true)
    	else(home === false)		
    		document.getElementById("homePage").style.display='block';
    };
    
    checkPage(contact, projects, about, home);
    </style>

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    What do you expect to occur?
    you have 3 else statements following 1 if, I believe you require else if, and code to execut
    Code:
    var checkPage = function (contact, projects, about, home)
    { 
    	if (contact === true){}
    	else if(projects === true){}
    	else if(about === true){}
    	else(home === false)	{	
    		document.getElementById("homePage").style.display='block';
            }
    };
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    themindco (03-09-2012)

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    this line tests whether the element exists.
    Code:
    var contact = document.getElementById("contactPage") ? true:false;
    Do you expect it to test the value of an input field?
    Code:
    var contact = document.getElementById("contactPage").value==='true' ? true:false;
    Last edited by blaze4218; 03-09-2012 at 03:27 PM. Reason: lol to jmrker :) copy/paste and I are close friends. We go way back!
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    themindco (03-09-2012)

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts
    It appears that you are checking for an element to exist, not the value of the element.

    It is not clear what the element is (checkbox, radio button, textbox, select, etc)
    so this is just a sample guess:
    Code:
    var contact = (document.getElementById("contactPage").selectedIndex == '1')  ? true:false;
    var projects = (document.getElementById("productPage").value != '') ? true:false;
    var about = (document.getElementById("aboutPage").checked) ? true:false;
    var home = (document.getElementById("homePage").checked == false) ? true:false;
    Dang, that blaze4218 types fast!!!
    Last edited by jmrker; 03-09-2012 at 03:21 PM. Reason: Too slow!

  • Users who have thanked jmrker for this post:

    themindco (03-09-2012)

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Apologize. Should have been more precise to what I am doing. It is for nav of the 'section' element.

    example at: http://themindspot.com


    Trying suggestions now.

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    <style type="text/javascript"><style type="text/javascript">
    maybe
    <script type="text/javascript">
    you need an onload handler to
    run the getElementsById code in
    maybe
    <body onload="init()">

    then put your code in
    the function
    Code:
    function init(){
    var contact = (document.getElementById("contactPage")) ? true:false;
    var projects = (document.getElementById("productPage")) ? true:false;
    var about = (document.getElementById("aboutPage")) ? true:false;
    var home = (document.getElementById("homePage")) ? true:false;
    
    }
    Last edited by DaveyErwin; 03-09-2012 at 03:53 PM.

  • Users who have thanked DaveyErwin for this post:

    themindco (03-09-2012)

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts
    After attempting a few things I would have thought this wold worked but no go.

    Code:
    <script>
    var contact = (document.getElementById("contactPage").style.display='block') ? true:false;
    var projects = (document.getElementById("productPage").style.display='block') ? true:false;
    var about = (document.getElementById("aboutPage").style.display='block') ? true:false;
    var homeP = (document.getElementById("homePage").style.display='block') ? true:false;
    
    var checkPage = function (contact, projects, about, homeP)
    { 
    	if (contact === true){}
    	else if(projects === true){}
    	else if(about === true){}
    	else if(homeP === false){		
    		document.getElementById("homePage").style.display='block';}
    };
    </script>
    and

    Code:
    <body onLoad="checkPage(contact, projects, about, homeP);">
    Last edited by themindco; 03-09-2012 at 04:02 PM.

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    You are setting the value. Look at my example again for a conditional test
    Code:
    var contact = (document.getElementById("contactPage").style.display='block') ? true:false;
    should be
    Code:
    var contact = (document.getElementById("contactPage").style.display=='block') ? true:false;
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    themindco (03-09-2012)

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Again, I still don't understand what you expect to accomplish...

    You have left the code brackets for 3 of your conditional tests empty... why?
    Should that be
    Code:
    <script>
    var contact = (document.getElementById("contactPage").style.display=='block') ? true:false;
    var projects = (document.getElementById("productPage").style.display=='block') ? true:false;
    var about = (document.getElementById("aboutPage").style.display=='block') ? true:false;
    var homeP = (document.getElementById("homePage").style.display=='block') ? true:false;
    
    var checkPage = function (contact, projects, about, homeP)
    { 
    	if (contact === true){		
    		document.getElementById("contactPage").style.display='block';}
    	else if(projects === true){		
    		document.getElementById("productPage").style.display='block';}
    	else if(about === true){		
    		document.getElementById(aboutPage").style.display='block';}
    	else if(homeP === false){		
    		document.getElementById("homePage").style.display='block';}
    };
    </script>
    ?
    Last edited by blaze4218; 03-09-2012 at 04:23 PM. Reason: I didn't follow my own advise! see blue changes... oops
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #10
    New Coder
    Join Date
    Mar 2012
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I had noticed I did that between post and thought it wold have been the problem but nope again.

    Made the following modifications:

    Code:
    <script>
    var contact = (document.getElementById("contactPage").style.display == 'block') ? true:false;
    var projects = (document.getElementById("productPage").style.display == 'block') ? true:false;
    var about = (document.getElementById("aboutPage").style.display == 'block') ? true:false;
    
    var checkPage = function (contact, projects, about)
    { 
    	if (contact === true){}
    	else if(projects === true){}
    	else if(about === true){}
    	else {	
    		document.getElementById("homePage").style.display='block';}
    };
    </script>
    and

    Code:
    <body onLoad="checkPage(contact, projects, about);">
    Figured no need to check for 'homePage'.
    Last edited by themindco; 03-09-2012 at 04:21 PM.

  • #11
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    When I ask "what do you expect to accomplish?" a link to what you have doesn't answer that. It shows me what you have, and since you claim that the code is *not* working for you, I must assume that what is at the link is not what you expect to accomplish... When I view the page it appears to me that it is working correctly, so what would you like addressed?

    (I suppose, if all you wanted was a correction of your code as posted- you have gotten that.)

    Also, I don't see that you have made the corrections to your site that you claim to have made...
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I can't believe I missed this...

    Did you make DaveyErwin's suggested change from post #7? You are running javascript that operates against DOM elements before they are loaded! You must do that after the body loads.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #13
    New Coder
    Join Date
    Mar 2012
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I apologize if I am frustrating you. What I am trying to accomplish is when the page loads to check if any 'section id's' are currently being set to block. If they are then they do nothing. That's why the if statement is like so. If any statement is true then it does nothing.

    And yes you have helped very much. Thank you! I do apologize for not saying so earlier.

  • #14
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    It is not necessary to write javascript to execute nothing. Nothing will occur all by itself.

    If you would like do something if the value is *not* currently set to block, then you are doing something. I believe this would be of greater value to you.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #15
    New Coder
    Join Date
    Mar 2012
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Ok. It works for the most part but now the homePage wont go away.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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