0

I want to read all page inputs ids and values into object and pass it to function which loop throuhg them and extract id and value.

I started with:

function send()
{
var data = [];
var inputs = $(":text");
for (var i = 0, l = inputs.length; i < l; i++) {
   var input;
   input.id = inputs[i].attr("id");
   input.text = inputs[i].val();
   data[i] = input;
}
receive(data);
}

function receive(data)
{
    for (var input in data) {
        alert(input.id);
        alert(input.text);        
    }
}

Why this does not work?

5 Answers 5

3

If you give your input elements a name attribute, you could just do this...

var data = $(":text").serializeArray();

...which results in this data structure (taken from the docs)...

[
  {
    name: "a",
    value: "1"
  },
  {
    name: "b",
    value: "2"
  },
  {
    name: "c",
    value: "3"
  },
  {
    name: "d",
    value: "4"
  },
  {
    name: "e",
    value: "5"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

1

For adding items to an array, you likely want to use .push() instead of accessing it via the numeric index. Also, jQuery provides a .each to iterate over a set.

var data = [];
$(':input:not(:button)').each(function() {
   data.push({
      id: this.id,
      text: $(this).val()
   });
});

One more thing. jQuery does include a .map function, which returns an array "mapped" over a given set which could shorten the above code down to...

var data = $(':input:not(:button)').map(function() {
    return { id: this.id, text: $(this).val() };
});

1 Comment

you forgot closing bracket in first example
0

Use jquery's serializeArray function

HTML:

<form id="test">
    <input type="hidden" name="a1" value="test" />
    <input type="text" name="a2" value="test2" />
    <input type="text" name="a3" value="test2" />
    <input type="text" name="a4" value="test2" />
</form>

JS:

function getData(){
    var data = $("#test").serializeArray();
    for(var i in data){
       alert(data[i].name+":"+data[i].value);   
    }
}

jsFiddle example: http://jsfiddle.net/k2Dd4/

Comments

0

I am not sure of the exact implementation in jQuery, but in Javascript I would do it as

var inputs = document.getElementsByTagName("input");
var textelems;

for (var i=0;i<inputs.length;i++) {
    if (inputs[i].type == "text") {
        tempElem = [];
        tempElem['id'] = inputs[i].id;
        tempElem['value'] = inputs[i].value;
        textelems.push(tempElem);
    }
}

This would give you a 2D-array (tempElems) where each index is a textbox with its own indexes of id and value.

Comments

0

Correct code would look like:

function send()
{
var data = [];
var inputs = $(":text");
for (var i = 0, l = inputs.length; i < l; i++) {
    var input = {};
    input.id = $("#" + inputs[i].id).attr("id");
    input.text = $("#" + inputs[i].id).val();            
    data[i] = input;
}
receive(data);
}

function receive(data)
{
    for (var i = 0, l = data.length; i < l; i++) {
       alert(data[i].id);
       alert(data[i].text);
    }
}

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.