Rocket
Rocket

Reputation: 1093

Jquery working with returned data

I have a simple page that has a dropdown list on it.

When the list is changed I'm using jquery / ajax to query a database and return a set of values. This is working and the values are being returned in this format :

"abc=12.3|bc=3.14.3|ef=231.23|pol=891.42|lki=2.33.2|adr=4.4.4"

I'm spliting that string on | which results in the this :

["abc=12.3", "bc=3.14.3", "ef=231.23", "pol=891.42", "lki=2.33.2", "adr=4.4.4"] 

How do I loop through this output and assign the value after the = to the matching ID. Each ID is the value before the equals. "ID=val"

EG: I want to update id 'abc' with 12.3, id 'ef' with 231.23 and so on.

Thanks

Upvotes: 1

Views: 27

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

You need to loop over your array, then split once more on the = to get the id and value. Something like this:

var data = ["abc=12.3", "bc=3.14.3", "ef=231.23", "pol=891.42", "lki=2.33.2", "adr=4.4.4"];
for (var i = 0; i < data.length; i++) {
    var fieldData = data[i].split('=');
    $('#' + fieldData[0]).val(fieldData[1]);
}

Upvotes: 3

Related Questions