youaremysunshine
youaremysunshine

Reputation: 351

put javascript variable into array and do foreach loop

I assigned a lot of variable in javascript and i wish to store these into array and do the looping like foreach in javascript. How should I do this?

var name=document.forms["form"]["name"].value;
    var email=document.forms["form"]["email"].value;
    var mobile=document.forms["form"]["mobile"].value;
    var q1=document.forms["form"]["q1"].value;
    var q2=document.forms["form"]["q2"].value;
    var q3=document.forms["form"]["q3"].value;
    var l1=document.forms["form"]["logo1"].value;
    var l2=document.forms["form"]["logo2"].value;
    var l3=document.forms["form"]["logo3"].value;
    var p1=document.forms["form"]["photo1"].value;
    var p2=document.forms["form"]["photo2"].value;
    var p3=document.forms["form"]["photo3"].value;

    if ( name == "" ) {
        alert("Please fill up all field to submit!");
        $('#name_error').css({'display': 'block'});
        return false;
    } else if ( email == "" ) {
        alert("Please fill up all field to submit!");
        $('#email_error').css({'display': 'block'});
        return false;
    }

Upvotes: 0

Views: 6305

Answers (4)

Annihil
Annihil

Reputation: 151

This might do what you want?

var array = [];
array.push({ name: "name", value: document.forms["form"]["name"].value});
array.push({ name: "email", value: document.forms["form"]["email"].value});
array.push({ name: "mobile", value: document.forms["form"]["mobile"].value});
// add other values here ...

array.forEach(function (obj) {
    if (obj.value == "") {
        alert("Please fill up all field to submit!");
        $("#" + obj.name + "_error").css({ "display": "block" });
        return false;
    }
});

Unfortunately, we need to store the name of the element in addition to its value in the array, so we can access the right error-element for it.

You could take a look at http://jqueryvalidation.org/ for validation

EDIT:

// I think we only need the element names and then get the value in the loop
var array = [];
array.push("name");
array.push("email");
array.push("mobile");
// add other values here ...

array.forEach(function (name) {
    if (document.forms["form"][name].value == "") {
        alert("Please fill up all field to submit!");
        $("#" + name + "_error").css({ "display": "block" });
        return false;
    }
});

EDIT 2: According to rene's comment: If the function returns false, there should be no submit. Hope i did everything alright this time ;)

$("#form").on("click", "#submitbutton", function(e) {
    e.preventDefault();
    var submit = true,
        array = [];

    array.push("name");
    array.push("email");
    array.push("mobile");
    // add other values here ...

    array.forEach(function (name) {
        if (document.forms["form"][name].value == "") {
           alert("Please fill up all field to submit!");
            $("#" + name + "_error").css({ "display": "block" });
            submit = false;
            return false;
        }
    });
    return submit;
});

Upvotes: 3

GGJ
GGJ

Reputation: 644

This will create an object you could loop through

var values = {
    name: document.forms["form"]["name"].value,
    email: document.forms["form"]["email"].value,
    mobile: document.forms["form"]["mobile"].value,
    q1: document.forms["form"]["q1"].value,
    q2: document.forms["form"]["q2"].value,
    q3: document.forms["form"]["q3"].value,
    l1: document.forms["form"]["logo1"].value,
    l2: document.forms["form"]["logo2"].value,
    l3: document.forms["form"]["logo3"].value,
    p1: document.forms["form"]["photo1"].value,
    p2: document.forms["form"]["photo2"].value,
    p3: document.forms["form"]["photo3"].value
};

for ( var item in values ) {
    console.log( item + ': ' + values[ item ];
    // do something
}

if ( values.email === '' ) {
    // conditional use here
}

Upvotes: 0

Markus
Markus

Reputation: 202

I created a fiddle to show you how to do it: http://jsfiddle.net/markus_b/rtRV3/

HTML:

<form name="form">
    <input type="text" name="name"/>
    <input type="text" name="email"/>
    <input type="text" name="mobile"/>
</form>

JS:

for (var i = 0; i < document.forms["form"].length;i++) {
    if (document.forms["form"][i].value == "")
        alert(document.forms["form"][i].name + " is empty!");
}

Basically you step through all the elements and query if they are empty.

Upvotes: 0

Andy897
Andy897

Reputation: 7133

var a = 1;
var all = new Array();
for(i=0;i<4;i++)
{
all[i]=a;
a++;
}

Replace 4 with number of fields and a with your document.get Similarly you can access them.

Upvotes: -1

Related Questions