What is the purpose of $(elem)
and when do I use it?
I have a problem with my javascript function, I have 3 forms and this 3 forms sometimes share the same field classes, thus when I try to do a verification for 1 of the form even though it is properly filled up it will return an error since the function will get the class of the other form, one of my friend said to use $(elem) since it worked for his problem, but I do not know how to use $(elem), when to use it, and how does it work.
It would be really great if anyone could explain this to me.
an example would also be appreciated :)
Example:
function dispFields(a, b, c, z, ch)
{
var valid;
var blanks = Array();
var blanks2 = Array();
var email_reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
var myVal = $('#'+a).find('input.required').map(function() {
return $(this).val();
}).get().join(',');
var myTitle = $('#'+a).find('input.required').map(function() {
return $(this).attr("title");
}).get().join(',');
var myEmail = $('#'+a).find('input.email-field').map(function() {
return $(this).val();
}).get().join(',');
var myEmailtitle = $('#'+a).find('input.email-field').map(function() {
return $(this).attr("title");
}).get().join(',');
var email_val = myEmail.split(',');
var email_title = myEmailtitle.split(',');
var email_error = email_val.length;
var error_form = myVal.split(',');
var error_title = myTitle.split(',');
var errorlength = error_form.length;
String.prototype.email_match = function(s){
return this.match(s)!==null
}
for(y=0;y<email_error;y++){
if (!('indexOf' in Array.prototype)) {
Array.prototype.indexOf= function(finds, is /*opt*/) {
if (is===undefined) is= 0;
if (is<0) is+= this.length;
if (is<0) is= 0;
for (var n= this.length; is<n; is++)
if (is in this && this[is]===finds)
return is;
return -1;
};
}
if((error_title.indexOf(email_title[y])) == -1){
if(email_val[y] == ''){
valid = true;
}else{
if(email_reg.test(String(email_val[y]).toLowerCase())==false){
blanks2[y] = email_title[y]+" - Should be valid email";
valid = false;
}
}
}
}
for(x=0;x<errorlength;x++){
if(error_form[x] == ''){
if(myTitle == ''){
valid = true;
}else{
blanks[x] = "Required - "+error_title[x];
valid = false;
}
}else{
var myBool = (String(error_title[x]).toLowerCase()).email_match("email");
if(myBool == true){
if(email_reg.test(String(error_form[x]).toLowerCase())==false){
blanks[x] = error_title[x]+" - Should be valid email";
valid = false;
}
}else{
if(blanks == ''){
valid = true;
}
}
}
}
if(blanks == '' && blanks2 == ''){
valid = true;
}else{
valid = false;
}
var blankc = blanks.concat(blanks2);
var requiredS = blankc.toString();
var nblank = requiredS.split(',').join("\n");
if(valid != true){
alert("Please review:\n\n"+nblank);
document.getElementById(a).style.display = "";
document.getElementById(b).style.display = "none";
}else{
var ssplits = z.split(',');
var fieldlengths = ssplits.length;
var indexes = fieldlengths - 1;
var d;
var e;
d=parseInt(c,10)+1;
e=d-1;
if(b == ssplits[indexes]){
$("#step"+d).addClass("active");
$("#step"+e).removeClass("active");
document.getElementById(a).style.display = "none";
document.getElementById(b).style.display = "";
document.getElementById("tbutton").style.display = "";
document.getElementById("tcont").style.display = "none";
document.getElementById("captchas").style.display = "none";
}
$("#step"+d).addClass("active");
$("#step"+e).removeClass("active");
document.getElementById(a).style.display = "none";
document.getElementById(b).style.display = "";
if(b == ch){
document.getElementById("captchas").style.display = "";
}
}
return false;
}
Now this function is called whenever i submit a form, but in a page i can have 2 or 3 forms that may have the class "required" thus even if the fields in the form is filled up it will still alert an error because it is getting the classes of the fields of the other forms.