0

I would like to create a JSON string using multiple input fiels with same cla. Allow me to show you my HTML for explanation

<div>
    Email:<input title="QA" type="text" class="email">
    Name: <input type="text" class="name">
</div>

<div>
    Email:<input title="DEV" type="text" class="email">
    Name: <input type="text" class="name">
</div>

<div>
    Email:<input title="PROD" type="text" class="email">
    Name: <input type="text" class="name">
</div>

Currently my JavaScript is as follows:

 var ServerUser = [];

 $("input[class=email]").each(function() {
    var id = $(this).attr("title");
    var email = $(this).val();

    tmp = {};
    tmp['id '] = id;
    tmp['email '] = email;

    ServerUser.push(tmp);
 });

However what it produces the following result

[{title: QA, email: '[email protected]'}, {title: PROD, email: '[email protected]'},{title: DEV, email: '[email protected]'}]

What I WOULD like to do is as follows

[{title: QA, email: '[email protected]', name: 'Paul'}, {title: PROD, email: '[email protected]', name: 'Mark'},{title: DEV, email: '[email protected]', name: 'Mike'}]

Where the name value would come from the respective Name input field.

How do I approach this problem? Thank you for your help and reading this.

Regards.

2
  • how can that code possibly create json with the keys title and email when you are clearly using TaskID and ClientEmail in your js? I assume you simplified it a little. You'll need to get the name inside of the same each that is getting the email and title. Commented Feb 21, 2013 at 19:30
  • @KevinB oops, my mistake. Fixed it. Commented Feb 21, 2013 at 19:33

3 Answers 3

5
var inputs = $('input.email'), tmp;
$.each(inputs, function(i, obj) {
  tmp = {
    'title': $(obj).attr('title'),
    'email': $(obj).val(),
    'name' : $(obj).siblings('.name').val()
  };

  ServerUser.push(tmp);
});

That should do it

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

1 Comment

works perfect! Thank you. You do have a slight typo on your code snippet at 'email': $(ob).val(), should be 'obj' for it to run perfectly :) Thanks again.
0

Why you just don't do it manually by inserting variables into prewrited string?

Rename the inputs by each's title.

Example:

var onejson = "[{title: QA, email: emailQA, name: nameQA}, {title: DEV, email: emailDEV, name: nameDEV}, {title: PRO, email: emailPRO, name: namePRO}];

I know it's a little steparound, but I think it's not bad solution and I would do it like I've described. Hope helps.

Comments

0

This is a poor programming practice, because your input fields should have decipherable unique identifiers you can use to get their values, but for your example using the Next Sibling Selector will allow you to select the next input element in your div's.

example:

 var ServerUser = [];
    $("input[class=email]").each(function() {

    var id = $(this).attr("title");
    var email = $(this).val();
    var name = $(this).parent().first().find('input:last').val();

    tmp = {};
    tmp['TaskID'] = id;
    tmp['ClientEmail'] = email;
    tmp['ClientName'] = name;

    ServerUser.push(tmp);
 });

This is a long selector, and there's probably a better way to structure your form so you don't need to do this, but for as long as the name input is the last one you're going to have, this will work.

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.