I need to make a if statement with in a function that returns multi-values. I tried this but it’s not working.
a(this).attr('src', function(i, current){
if ( i == 'http://www.old.com'){
return current.replace('http://www.old.com','http://www.new.com');
}
else if ( i == 'http://www.older.com'){
return current.replace('http://www.older.com','http://www.new.com');
}
else ();
});
The format of jQuery’s callback-based
.attrfunction isYour
ifstatements use the first callback argument,i, which will always be the index number, but you seem to expect them to be strings. However, the string value of each attribute is in the second callback argument. You probably meant to compare the second argument,current.Furthermore (based on a comment you made), you don’t actually want to do an exact match for
old.com, you want to see if the URL starts withhttp://www.old.com: