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 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Changing CheckBox With a VAR [Should be Easy - Please Help]

    I tired to slim this down as much as I could..... I'm trying to make a function that will change the checkboxs that I pass to it..

    <script type="text/javascript">
    function checkBoxValidate(Engine_Var){
    document.new_car_script.Engine_Var.checked = true;
    }


    The check box is named Engine3 and the variable is getting passed properly from an alert that I had in there. If I change Engine_Var to Engine3 it works.

    Please Help - Jutboy

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm.. try something like this.

    Code:
    <script>
         function checkBoxValidate(Engine_Var){
              var CheckboxElement = eval("document.new_car_script." + Engine_Var);
              CheckboxElement.checked = true;
         }
    </script>

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Your the freaking man!!!!!

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    haha... glad i could help and welcome to the board.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    eval method is not efficient and is confusing for new coders.
    Try this instead:
    Code:
    function checkBoxValidate(Engine_Var){
      document.new_car_script.elements[Engine_Var].checked = true;
    }
    I've used the javascript square bracket notation. See my sig for more info on this. This technique is very useful.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    whats not efficient about it? not saying your wrong, just courious. I've used it plenty without a problem.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    This has been discussed lengthily here in CF.

    http://www.codingforums.com/showthread.php?t=20143
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow, this board is great....lots of info here

    This is actually my code... I put in php so I could just loop it and save myself lots of time.....its designed for lots and lots of parts.

    Code:
    echo "<script type='text/javascript'>";
    echo "function checkBoxValidate(part0,condition0,part1,condition1,part2,condition2,part3,condition3){";
    
    for ($i=0;$i<=3;$i++){
    echo "var CheckboxElement = eval('document.new_car_script.' + part$i + '[' + condition$i + ']');";
    echo "CheckboxElement.checked = true;";
    The thing is I couldn't get glenngv's method to work....seems js doesn't like you putting a variable/or combining variables/strings into a document command? i would like to be as efficent as possible however. Please advise.

    Thanks - Jman

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    echo "var CheckboxElement = document.new_car_script.elements[part$i][condition$i];";
    I don't know php so I may have inserted the $i variable incorrectly.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #10
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Your the man too!!!! You basically got it....

    Code:
    echo "var CheckboxElement = document.new_car_script.elements[part$i"."][condition$i"."];";
    echo "CheckboxElement.checked = true;";
    Hehe......Please notice I gave both of you rep points!


  •  

    Posting Permissions

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