Using jQuery, I’m attempting to iterate over an array and compare to a value, but am running into issues that are stumping me. Here’s the code:
var cF = ['first','last','email'];
var fFound = 0;
cj(this).children('.section').each(function(f){
var fId = cj(this).attr('class');
fId = fId.replace('-section', '');
console.log('fId: ',fId);
cj.each(cF, function( cFi, cFv ){
console.log('cFv: ',cFv);
if ( cFv == fId ) {
console.log('found!');
fFound = 1;
}
});
console.log('fFound: ',fFound);
});
Basically what I’m doing is searching through a series of divs (passed with ‘this’), retrieving the class name, stripping the suffix to the class, and then comparing it with my cF array. If found, I want to set fFound to 1 (it’s my flag to indicate the value is present).
Originally I was using jQuery.inArray() but wasn’t having much luck. When I send fId and cFv to the console — they match — but the comparison doesn’t recognize them as matching.
Try this