darrylm
10-30-2012, 12:18 PM
I am attempting to create a function that follows similar logic to the following:
function ___ (array1, array2) {
if( for (i in array1) {
$("#"+array1[i]).val() != ""}){
for (i in array2) {
$("#"+array1[i]).show()}
}
}
I want to feed an array of values into a function, every value in the array must act as an extra condition in an IF statement.
Is it possible?
My best attempt was the next chunk of code which fell flat.
I attempted to create a string representing the condition, but the IF treats the var as the condition( therefore testing its existence), not the value it represents.
$(document).ready(function(e) {
var q = ['fname', 'lname'];
_____(q, "");
});
function _____(q, a){
// Initialize the condition
var condition = "";
// Create a count for conditions
var count = 0;
// Loop through array containing IDs to be included in the condition
for ( i in q ) {
// If the condition is not the first, add &&
if(count == 0) {
condition += '$("#' + q[i] + '").val() != "" ';
} else {
condition += '&& $("#' + q[i] + '").val() != ""';
}
// Increment count
count++;
}
}
I apologise if the above isnt as clear as it should be.
*There is jQuery included in that code
function ___ (array1, array2) {
if( for (i in array1) {
$("#"+array1[i]).val() != ""}){
for (i in array2) {
$("#"+array1[i]).show()}
}
}
I want to feed an array of values into a function, every value in the array must act as an extra condition in an IF statement.
Is it possible?
My best attempt was the next chunk of code which fell flat.
I attempted to create a string representing the condition, but the IF treats the var as the condition( therefore testing its existence), not the value it represents.
$(document).ready(function(e) {
var q = ['fname', 'lname'];
_____(q, "");
});
function _____(q, a){
// Initialize the condition
var condition = "";
// Create a count for conditions
var count = 0;
// Loop through array containing IDs to be included in the condition
for ( i in q ) {
// If the condition is not the first, add &&
if(count == 0) {
condition += '$("#' + q[i] + '").val() != "" ';
} else {
condition += '&& $("#' + q[i] + '").val() != ""';
}
// Increment count
count++;
}
}
I apologise if the above isnt as clear as it should be.
*There is jQuery included in that code