Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-25-2007, 06:04 PM   PM User | #1
Jutboy
New Coder

 
Join Date: Feb 2007
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Jutboy is an unknown quantity at this point
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
Jutboy is offline   Reply With Quote
Old 02-25-2007, 09:08 PM   PM User | #2
JayStang
Regular Coder

 
Join Date: Jan 2006
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
JayStang is an unknown quantity at this point
hmmm.. try something like this.

Code:
<script>
     function checkBoxValidate(Engine_Var){
          var CheckboxElement = eval("document.new_car_script." + Engine_Var);
          CheckboxElement.checked = true;
     }
</script>
JayStang is offline   Reply With Quote
Old 02-25-2007, 11:45 PM   PM User | #3
Jutboy
New Coder

 
Join Date: Feb 2007
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Jutboy is an unknown quantity at this point
Your the freaking man!!!!!
Jutboy is offline   Reply With Quote
Old 02-26-2007, 12:50 AM   PM User | #4
JayStang
Regular Coder

 
Join Date: Jan 2006
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
JayStang is an unknown quantity at this point
haha... glad i could help and welcome to the board.
JayStang is offline   Reply With Quote
Old 02-26-2007, 04:15 PM   PM User | #5
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-26-2007, 07:35 PM   PM User | #6
JayStang
Regular Coder

 
Join Date: Jan 2006
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
JayStang is an unknown quantity at this point
whats not efficient about it? not saying your wrong, just courious. I've used it plenty without a problem.
JayStang is offline   Reply With Quote
Old 02-27-2007, 06:58 PM   PM User | #7
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
This has been discussed lengthily here in CF.

http://www.codingforums.com/showthread.php?t=20143
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-27-2007, 09:12 PM   PM User | #8
Jutboy
New Coder

 
Join Date: Feb 2007
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Jutboy is an unknown quantity at this point
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
Jutboy is offline   Reply With Quote
Old 02-27-2007, 09:27 PM   PM User | #9
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-27-2007, 10:04 PM   PM User | #10
Jutboy
New Coder

 
Join Date: Feb 2007
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Jutboy is an unknown quantity at this point
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!
Jutboy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:49 PM.


Advertisement
Log in to turn off these ads.