The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
String in array statement not working
Discuss String in array statement not working in the JavaScript Development forum on Dev Shed. String in array statement not working JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 31st, 2007, 10:34 AM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
String in array statement not working
Depending on the combinations of checkboxes i choose, different message will appear to the user. I think the main problem is how i am declaring the if .. then statements where the condition is 'something' in cvals && 'something else' in cvals.
The goal of this script is to obtain values from the checkboxes when they are checked and the values are put into an array. If the array contains certain combinations of 'key words' then a certain message will appear.
Here is the javascript :
Javascript Code:
Original
- Javascript Code |
|
|
|
function checkClick() { var message; var cvals = new Array(); var x = document.escorial.getElementsByTagName('input'); var aa = document.escorial.identGene; // Loop for all checkboxes in form named escorial for ( var i = 0; i < x.length; i++ ) { if ( x.type == 'checkbox' ) { if ( x[i].checked ) { cvals.push(x[i].name && x[i].value); } } } // If statement for radio button. if ( aa.value == 'Yes' ) { cvals.push(aa.name && aa.value); } //Begins the if statements determining the message if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && (('bulbar' in cvals && 'cervical' in cvals && 'thoracic' in cvals) || ('bulbar' in cvals && 'cervical' in cvals && 'lumbosacral' in cvals)) ) { message = 'Definate ALS'; } else if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && 'bulbar' in cvals ) { message = 'Probable ALS'; } else if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && 'EMG' in cvals ) { message = 'Probable ALS, Lab Supported'; } else if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && 'Yes' ) { message = 'Definite Familial ALS, Lab Supported'; } else if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals ) { message = 'Possible ALS'; } else if ( 'Upper Motor Neuron' in cvals || 'Lower Motor Neuron' in cvals ) { message = 'Suspected ALS'; } else { message = ''; } //Redisplays the message with the html document.getElementById('message').innerHTML = message; }
Last edited by luckybwild : July 31st, 2007 at 10:38 AM.
|

July 31st, 2007, 10:49 AM
|
 |
Meatball Surgeon
|
|
Join Date: Jul 2004
Location: Elbow deep in code
|
|
|
So what problem are you running into?
__________________
Three gigs for the secretaries fair
Seven gigs for the system source
Nine gigs for the coders in smoky lairs
One disk to rule them all, one disk to bind them
One disk to hold the files, and in the darkness grind'em
---------------------------------------------------
It is by caffeine alone that I set my mind in motion.
It is by the beans of Java, that my thoughts acquire speed.
The hand acquire shakes; the shakes become a warning.
It is by caffeine alone that I set my mind in motion.
|

July 31st, 2007, 10:55 AM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
nothing happens when i click the checkboxes. even the (most basic) last else if statement stating:
else if ( 'Upper Motor Neuron' in cvals || 'Lower Motor Neuron' in cvals )
doesn't work. That would output a message if i click one of the checkboxes that has a value of either UMN or LMN should it not?
|

July 31st, 2007, 11:12 AM
|
 |
Meatball Surgeon
|
|
Join Date: Jul 2004
Location: Elbow deep in code
|
|
|
I think it is you giant if statement of doom (however, it looked OK) did you verify that there was data in your array?
|

July 31st, 2007, 12:46 PM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
either i can't display an array properly, or the function just hangs.
i simply put document.write(cvals); and when i check a box it goes blank and shows that its loading something, but doesn't do anything
|

July 31st, 2007, 04:57 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
1) You're using the "in" operator incorrectly. I recommend that you use Array.indexOf() instead. You'll need to use the function posted on that page to support browsers that don't support that method of Array yet.
2) Why are you doing the following and what do you expect the result to be?
Code:
cvals.push(x[i].name && x[i].value);
3) document.write() can only be used while the page is loading -- if it isn't it will replace the page with what ever it writes out. The Document Object Model (DOM) can be used to add elements, text, etc. to the page without reloading it.
|

July 31st, 2007, 05:26 PM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
yea i had initially set:
document.getElementById('message').innerHTML = message;
to get the message displayed, but nothing happens.
my main thing i want to do is somehow store all the values that have been set (by checking) and if certain values are stored in the array then different messages will be displayed.
i will look into that link you gave me.
|

July 31st, 2007, 05:32 PM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
I realize my
cvals.push(x[i].name && x[i].value);
isn't very helpful. I should probably do something like:
cvals[i] = x.name ', ' x.value;
would that work just as well?
|

July 31st, 2007, 05:36 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
Code:
cvals.push(x[i].name && x[i].value);
will have the same result as
Code:
cvals.push(x[i].value);
perhaps you want
Code:
cvals.push(x[i].name+', '+x[i].value);
|

July 31st, 2007, 05:39 PM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
yea i need both the value and the name because both factors affect which message is displayed.
|

July 31st, 2007, 05:56 PM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
i changed it to:
Javascript Code:
Original
- Javascript Code |
|
|
|
cvals.push(x[i].name + ', ' + x[i].value);
but still no message. the last line that i relay the message to is <span id="message"></span>. and i have onclick = "checkClick()" within each checkbox tag as well as the radio button.
does everything you think make at least logical sense?
|

July 31st, 2007, 06:04 PM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
i am trying out the indexOf function right now.
|

August 1st, 2007, 08:49 AM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
|
So i started messing around with the indexOf function and I finally get a message when i click a checkbox, but it displays the message only because the value of indices > -1.
I need to make each 'indices' variable exclusive to each string i.e 'Lower Motor Neuron', 'Upper Motor Neuron' .. etc. Since the while loop is set up to stop after lmn > -1 i don't get a true count of many of the indices of the string their are. I will mess around some more, but just throwing it out there. Thanks
|

August 1st, 2007, 09:00 AM
|
|
Contributing User
|
|
Join Date: Apr 2005
Posts: 149
  
Time spent in forums: 1 Day 21 h 10 m 37 sec
Reputation Power: 12
|
|
sorry forgot the code:
Javascript Code:
Original
- Javascript Code |
|
|
|
function checkClick() { var message; var cvals = new Array(); var x = document.escorial.getElementsByTagName('input'); var aa = document.escorial.identGene; // Loop for all checkboxes in form named escorial if ( x.type == 'checkbox' ) { for ( var i = 0; i < x.length; i++ ) { if ( x[i].checked == true) { cvals.push(x[i].name + ', ' + x[i].value); } } } // If statement for radio button. if ( aa.value == 'Yes' ) { cvals.push(aa.name && aa.value); } // Find all indices var indices = []; var lmn = cvals.indexOf('Lower Motor Neuron') while (lmn > -1) { indices.push(lmn); lmn = cvals.indexOf('Lower Motor Neuron', lmn + 1); } // var indices1 = []; // // var umn = cvals.indexOf('Upper Motor Neuron') // // while (umn) { // // indices1.push(umn); // umn = cvals.indexOf('Upper Motor Neuron', umn + 1); // } //Begins the if statements determining the message // if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && (('bulbar' in cvals && 'cervical' in cvals && 'thoracic' in cvals) || ('bulbar' in cvals && 'cervical' in cvals && 'lumbosacral' in cvals)) ) { // // message = 'Definate ALS'; // } // // else if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && 'bulbar' in cvals ) { // // message = 'Probable ALS'; // } // // else if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && 'EMG' in cvals ) { // // message = 'Probable ALS, Lab Supported'; // } // // else if ( 'Upper Motor Neuron' in cvals && 'Lower Motor Neuron' in cvals && 'Yes' ) { // // message = 'Definite Familial ALS, Lab Supported'; // } if ( indices > 0 ) { message = 'Possible ALS'; } else if ( indices > -1 ) { message = 'Suspected ALS'; } else { message = ''; } //Redisplays the message with the html document.getElementById('message').innerHTML = message; }
|

August 1st, 2007, 03:02 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
|
Huh? You seem to be confused.
The index does not matter here. You just need to check if it's -1 or not to see if the string exists in the array.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|