tags:

views:

21

answers:

1

Hi,

How can I split up xmlhttp.responseText I receive from a database select row by row? My database layout contains 3 columns which I want to hand over to a function.

This is what the responsetext looks like:

75px, 218px, foo, 12px, 13px, bar, 27px, 37px, bla

The function should be called:

myfunction(75px, 218px, foo) 

and so on.

Can anybody show me an example code?

A: 

You can "split" the text on the commas:

function handleResponse(rawResponse) {
  function trim(s) {
    return s.replace(/^\s*/, '').replace(/\s*$/, '');
  }

  var attrs = rawResponse.split(',');
  for (var i = 0; i < attrs.length; i += 3) {
    myFunction(trim(attrs[i]), trim(attrs[i + 1]), trim(attrs[i + 2]));
  }
}

edit to actually call the "trim" function I took the trouble to include :-)

Pointy
Isn't it neater to extend the `String` object with `String.prototype.trim`? ;)
Marcel Korpel
@Marcel well sure but I feel that it's a little dangerous to put stuff like that in Stackoverflow answers - you never know who'll find this question and just cut-paste the whole thing, and doing something like that could break unexpectedly
Pointy