0

Today I have a problem. I have a HTML Form, which can have a dynamic number of HTML Input Fields for an EMail Address. Via Javascript I want to evaluate those Data ans Post via XMLHttpRequest.

I fetch the Input fields like this:

var elements = document.getElementById("emailmodal").elements["multiple[]"];

Then I want to check, if it is an array, because if I have only one input field elements will not contain an array of elements, but only one element:

if(jQuery.isArray(elements))
{

    for(var i = 0; i < elements.length; i++)
    {

        formData.append("emailreceiver[]", elements[i].value);

    }

}else
{

    formData.append("emailreceiver[]", elements.value);

}

In case it is an Array, it is going through the Elements via For Loop, and if not, its directly taking the value.

However this doesnt work correctly. Even if I have multiple INputs, the Script tells me, its no array.

The Corresponding HTML is

<div class="form-group input-group">
<input type="text" name="multiple[]" class="form-control" value="<?php echo $kundendaten['Email']; ?>">
<span class="input-group-btn"><button type="button" class="btn btn-default btn-add">+</button></span>
</div>

This is from Bootstrap.

If i make var log to console and I have more then one element than it looks like this: enter image description here

If I make var log and only have one element it looks like this

enter image description here

What can I do?

Cheers, Niklas

5
  • Yes object is also an array of elements in JS because Javascript does'nt support associative arrays. Commented Apr 26, 2016 at 12:59
  • 2
    DOM Element doesn't have elements method. Can you share the output of elements ? Commented Apr 26, 2016 at 13:00
  • 1
    can't you just depend on the object length and if the length is greater than one, treat the object like an array? Commented Apr 26, 2016 at 13:01
  • I uploaded some pictures of var log. Commented Apr 26, 2016 at 13:05
  • I agree with @Mmcgowa3 you can easily just use $("form input.email").length and check if you have more than one input. Also you can keep a counter with a global variable and update it in the same function that adds additional input elements to your form Commented Apr 26, 2016 at 13:10

2 Answers 2

2

Thanks to Mmcgowa!!

if(elements.length > 1)
{

    alert("Array");
    for(var i = 0; i < elements.length; i++)
    {

        formData.append("emailreceiver[]", elements[i].value);

    }

}else
{

    alert("No Array");
    formData.append("emailreceiver[]", elements.value);

}

This works. If elements is no array, element.length will be undefined!

Sign up to request clarification or add additional context in comments.

Comments

0

I know you already have a solution, but why don't you simplify your code flow and do the following:

if (elements.length == 1) elements = [elements];

// now you know that if *elements* is not empty, it is always an array
for (var i = 0; i < elements.length; i++)
{
    formData.append("emailreceiver[]", elements[i].value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.