Results 1 to 8 of 8

Thread: if then else statement wont work

  1. #1
    Join Date
    Feb 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default if then else statement wont work

    hello

    im having trouble with an if-then-else statement. i have tried several ways to get it to work & i just cant get it to work. im not very experienced at this.

    i have 4 checkboxes that are checked & 5 textboxes for each color. if one of them is checked the totals from that color are supposed to be copied over to the matching color & textboxes. ie: if blue is checked then the the 5 scores are supposed to transfer over to the blue side. if i do each color seperatly with no if/then it works as a single function but when i applied all 4 functions(1 for each color) it no longer worked. so im at this point now & have tried several diff ways including using true or 'true' or case etc & i just cant get it. any help woudl be greatly appreciated.

    below is my code:
    Code:
    function swapT(){
      if(document.getElementById('checkboxB').checked){
          
       document.getElementById('copysum1b').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2b').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3b').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4b').value =  +document.getElementById('result4').value;
    document.getElementById('totalblue').value = document.getElementById('answerp1').value;
        }
        else if(document.getElementById('checkboxG').checked){
    
    document.getElementById('copysum1g').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2g').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3g').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4g').value =  +document.getElementById('result4').value;
    document.getElementById('totalgreen').value = document.getElementById('answerp1').value;
    }
    else if(document.getElementById('checkboxR').checked){
    
    document.getElementById('copysum1r').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2r').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3r').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4r').value =  +document.getElementById('result4').value;
    document.getElementById('totalred').value = document.getElementById('answerp1').value;
    }
    else {
    
    document.getElementById('copysum1y').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2y').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3y').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4y').value =  +document.getElementById('result4').value;
    document.getElementById('totalyellow').value = document.getElementById('answerp1').value;
    }
    }
    </script>
    HTML Code:
    <input type="button" onclick="swapT(); value="Next" tabindex="5"/>
    its driving me crazy!!!!
    nicole

  2. #2
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Please explain what you mean by "it no longer worked" Do you get an error message? is it the wrong color? What actually happens instead of it working properly?

  3. #3
    Join Date
    Feb 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks for replying.

    i have 2 columns. when you click a color on the left it reveals 4 boxes & a total box & checkbox thats already checked. after you enter all 4 values depending on which color you clicked the values should go to the right side under the same color. if i entered in numbers for green, they should go to green when i hit next etc... but no matter what color i click they always go to blue. the colors are listed in abc order ie: blue, green, red, yellow & the id's are named the same totalg1 or totalb1 etc to keep it simple.

    as i said, with no if/then i can give them all single functions on their own & they work. green goes to green, red to red etc. but then i have the problem of the next button showing all 4 functions. if i do it that way then when i click next the numbers dont make sence... some are on blue, some on green.

    i honestly dont see what is wrong wiht the code or why its not working. i have looked at so many if-then-else statements... & they look like mine it has to be something missing they they ALWAYS go to the 1st set of boxes any help is appreciated as i cant do the next step till i have this one working.

    thanks for your time.
    nicole

  4. #4
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nicole, the code looks fine are you sure the checkBoxes have different name values? Seems like they are all going to the first color because either the first checkbox is always checked and it never gets to the other if statements because the first is true or one or more of the name values are the same and therefore making the first if then true as well.

  5. #5
    Join Date
    Feb 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    im not sure if thats a good thing or a bad thing because now that the code is fine where is the problem. lol

    here is my code for the checkboxes.
    Code:
    <script type="text/javascript">
    
     function checkB() { 
     document.getElementById("checkboxB").checked = "checked"; 
     } 
     function checkG() { 
     document.getElementById("checkboxG").checked = "checked"; 
     } 
    function checkR() { 
     document.getElementById("checkboxR").checked = "checked"; 
     } 
    function checkY() { 
     document.getElementById("checkboxY").checked = "checked"; 
     } 
    
     </script>
    when they click the color, the checkbox also shows up checked for them. they are not checked by default. this is how i 'tried' to get the values in the other boxes. my reasoning is if bluecheckbox is checked then post values in the blue boxes. if green checked then post in green...

    everything works right now except the code from my if/then. i dont know loops or any other way to do the above code except by how i did it.

    thanks
    nicole

  6. #6
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nicole, check the name value of the checkboxes for instance if blue is checkboxB then the name of that checkbox value either in the html or via an ide in the value field will need to be filled out. If you copy and pasted the checkboxes they will all be named checkboxB instead of one being checkBoxB and the other checkboxG etc. they have to have individual names. Can you check that to see if that is the problem? What environment are you working in? Are you using some ide like eclipse or vidual studio? MS Excel? or just a text editor like notepad or wordpad?

  7. #7
    Join Date
    Feb 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks for replying. sorry i havent gotton to this sooner but i have been busy & havent had any free time.

    i plan on spending the next couple hours taking your advice & figuring this out. i use front page & notepad for editing.

    thanks,
    nicole

  8. #8
    Join Date
    Feb 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default not again!

    hello again.
    im happy to say i got that part working. everything was named correctly but you got me thinking when you said about the 1st checkbox always being checked. well du-uh me. so i added in some code to uncheck the box after the color is selected & voila it works great. thats whats so great about these forums, i didnt even think of that but someone else did. thanks for that.

    now im trying to use the same function & have the totals also show up with the correct color to later be sorted by score. i hope to have it all show up in the proper place already sorted since there are only 4 colors but im already over my head now... i have tried several diff ways of course & with the latest code i can get only the 1st color selected to show up. they all work if they are only the color selected ie: if another color was clicked even thou no numbers were entered it takes THAT color as the new color with the other colors numbers. so i figured i would just uncheck the other boxes upon hitting 'next' button & that would solve the problem but that doesnt work. strange thou, cause that was the solution to the earlier problem.

    Code:
    <script>
    
    function swapT(){
      if(document.getElementById('checkboxB').checked){
          
       document.getElementById('copysum1b').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2b').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3b').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4b').value =  +document.getElementById('result4').value;
    document.getElementById('totalblue').value = document.getElementById('answerp1').value;
    document.getElementById("bpawn1").style.visibility = "hidden";
     document.getElementById("checkboxB").checked = false;
     document.swap41.src='images/bpawn2.gif';
     document.getElementById('f1').value =  +document.getElementById('totalgreen').value;
    document.swap51.src='images/bpawn3.gif';
        }
        else if(document.getElementById('checkboxG').checked){
    
    document.getElementById('copysum1g').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2g').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3g').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4g').value =  +document.getElementById('result4').value;
    document.getElementById('totalgreen').value = document.getElementById('answerp1').value;
    document.getElementById("gpawn1").style.visibility = "hidden";
     document.getElementById("checkboxG").checked = false;
      document.swap42.src='images/gpawn2.gif';
       document.getElementById('f1').value =  +document.getElementById('totalblue').value;
       document.swap51.src='images/gpawn3.gif';
    }
    else if(document.getElementById('checkboxR').checked){
    
    document.getElementById('copysum1r').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2r').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3r').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4r').value =  +document.getElementById('result4').value;
    document.getElementById('totalred').value = document.getElementById('answerp1').value;
    document.getElementById("rpawn1").style.visibility = "hidden";
     document.getElementById("checkboxR").checked = false;
      document.swap43.src='images/rpawn2.gif';
       document.getElementById('f1').value =  +document.getElementById('totalred').value;
          document.swap51.src='images/rpawn3.gif';
    }
    else if(document.getElementById('checkboxY').checked){
    
    
    document.getElementById('copysum1y').value =  +document.getElementById('result1').value;
    	document.getElementById('copysum2y').value =  +document.getElementById('result2').value;
    document.getElementById('copysum3y').value =  +document.getElementById('result3').value;
    document.getElementById('copysum4y').value =  +document.getElementById('result4').value;
    document.getElementById('totalyellow').value = document.getElementById('answerp1').value;
    document.getElementById("ypawn1").style.visibility = "hidden";
    document.getElementById("checkboxY").checked = false;
     document.swap44.src='images/ypawn2.gif';
      document.getElementById('f1').value =  +document.getElementById('totalyellow').value;
         document.swap51.src='images/ypawn3.gif';
    }
    }
    </script>
    i also tried to compare them in another function for the scoring later by using operators but i just get errors for expected or unidentified. loooping is not my thing so i have to do it like this.
    Code:
     <script type="text/javascript">
            function placement(){
                var 1st = document.getElementById('f1').value;
                var 2nd = document.getElementById('f2').value;
                var 3rd = document.getElementById('f3').value;
                var 4th = document.getElementById('f4').value;
    
                if(1st < 2nd) && (1st < 3rd) && (1st < 4th){
                    var str="CLOSEST";
                
                document.getElementById('place').value =str;
            }
        </script>


    then this last code i tried & ggrrr its so frustrating to know what you want it to do but not be able to get it. thiscode shows them in the right spot but theres no sort... they show up out of order which is why i scrapped this to do the if/then above.
    Code:
      <script type="text/javascript">
    
      function movepoints(){
            
       document.getElementById('f1').value =  +document.getElementById('totalblue').value;
    	document.getElementById('f2').value =  +document.getElementById('totalgreen').value;
    document.getElementById('f3').value =  +document.getElementById('totalred').value;
    document.getElementById('f4').value =  +document.getElementById('totalyellow').value;
    
        }
      </script>
    <script type="text/javascript"> 
    
    function movepawns(){
      if(document.getElementById('f1').value == document.getElementById('totalblue').value); {
       document.swap51.src='images/bpawn3.gif';
    }
     if(document.getElementById('f1').value == document.getElementById('totalgreen').value); {
       document.swap52.src='images/gpawn3.gif';
    }
     if(document.getElementById('f1').value == document.getElementById('totalred').value); {
       document.swap53.src='images/rpawn3.gif';
    }
    if(document.getElementById('f1').value == document.getElementById('totalyellow').value); {
       document.swap54.src='images/ypawn3.gif';
    }
    
    }
        
    </script>
    i didnt attach any html because the problem is in the functions themselves just like last time. one button calls all the functions im having trouble with. agian i thank you for any help you give me. it seems so simple in my head... if this do that but in trying to apply it...

    thanks for your time
    nicole

Similar Threads

  1. Two scripts that wont work together
    By Mejse78 in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-31-2012, 09:38 AM
  2. Why wont this code work?
    By Demonicman in forum PHP
    Replies: 2
    Last Post: 11-09-2008, 08:55 AM
  3. Why wont this work?
    By Raztat in forum Java
    Replies: 4
    Last Post: 03-04-2008, 08:10 PM
  4. Why Wont This Work?!
    By Jas in forum CSS
    Replies: 6
    Last Post: 06-10-2007, 10:04 PM
  5. Script wont work
    By viper102464 in forum JavaScript
    Replies: 1
    Last Post: 07-23-2006, 11:04 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
  •