Smithey93
Smithey93

Reputation: 45

Group data by form

I have a page which displays some data from my database, how would i go about accessing this data with jQuery? By this, I mean grouping it by the row id from the database?

I was thinking about something like:

<input type="text" id="482983" name="reg" />
<input type="text" id="482983" name="somefield" />


<input type="text" id="482984" name="reg" />
<input type="text" id="482984" name="somefield" />

How could I pull that data into JSON like this:

{
    "482983":{"reg":"regvalue","somefield":"somefieldvalue"},
    "482984":{"reg":"regvalue","somefield":"somefieldvalue"}
}

Any ideas?

Upvotes: 0

Views: 51

Answers (1)

Kjeld Schmidt
Kjeld Schmidt

Reputation: 768

As mentioned, IDs should be unique. Instead, let them share a class, or a name, or let them be children of the same container with a unique ID. After you've done that, you can use jQuerys .each() to form it into a json or array object.

Upvotes: 1

Related Questions