0

I created multiplied input fields:

<div class="form-text-field first-name field-group">
    <input data-index="1" type="text" id="firstName1" class="signup-input firstName" name="first[1]" placeholder="">
</div>
<div class="form-text-field email field-group">
    <input type="text" data-index="1" id="inputMail1" class="signup-input text-value ignore" name="email[1]"  placeholder="${message(code:"signup.redesign.placeholder.eg.email")}"/>
    <span class="common-sprite disNone sign-up-cross first clone"></span>
</div>

I have a code in the JS file which clone every input.

I have to create arrays from the values of the inputs (one for the email, and one for the first name).

Here is the function:

var arrEmail = []
var arrName = []
function add() {
    var obj = {};
    var partner = {}
    $('.email input[type="text"]').each(function() {
        obj[this.data-index] = this.value;
    });
    arrEmail.push(obj)
    $('.first-name input[type="text"]').each(function() {
        partner[this.data-index] = this.value;
    });
    arrName.push(partner)
    console.log(arrEmail[0])
}

I didn't succeed to get the arrays in this code. How do I fix it?

8
  • identifiers can't have minus sign, try to use $(this).data('index'); Commented Apr 14, 2015 at 11:52
  • use $(this).data('index') or $(this).attr('data-index') Commented Apr 14, 2015 at 11:53
  • You can use map like arrEmail = $('.email input[type="text"], .first-name input[type="text"]').map(function({ return $(this).val(); }).get(); Commented Apr 14, 2015 at 11:54
  • Do you need the arrays index? Why don't you push elements to array? Like $('.email input[type="text"]').each(function(){ arrEmail.push($(this).val()); });. Commented Apr 14, 2015 at 11:56
  • @kmsdev- it's work:) but for some reason I got null value when I try to send the arr in ajax (data: {email: arrEmail, name: arrName}) Commented Apr 14, 2015 at 12:07

3 Answers 3

1

You have some mistakes.

  1. Wrong syntax in line $('.email input[type="text"]').each(function({. You forgot to close bracket.
  2. I don't understand why you tried get value this strange manner. You included jQuery. Use it!

I fix your code.

var arrEmail = [];
var arrName = [];

function add() {
    var obj = {};
    var partner = {};
    $('.email input[type="text"]').each(function(item) {
        obj[$(this).data('index')] = $(this).val();
    });
    arrEmail.push(obj);

    $('.first-name input[type="text"]').each(function() {
        obj[$(this).data('index')] = $(this).val();
    });
    arrName.push(obj);

    console.log(arrEmail);
    console.log(arrName);
}

$('#test').on('click', add);

jsFiddle demo

Upd #1

Haven't shown all conditions. Fixed it.

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

Comments

0

Use $(this).data('index') instead of this.data-index

Comments

0

You can handle this with a single array:

var myUsers= [];//this might be object that you store the user information

$('.first-name input[type="text"]').each(function(item) {
    //first create the user information
    var myUser = new Object();
    myUser.Username = $(this).val();
    myUser.Email= $(this).next().val();

    //then add the user information to the myUsers array
     myUsers.push(myUser );
});

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.