View Single Post
Old 01-29-2013, 04:25 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question

To 'OldPedant':

Please pardon the interruption.
I have been experimenting with you script of post #2.
I easily figured out how to expand it to 3 (or more) elements within the array.
Very helpful.

However, to test my understanding of the DOM nomemclature, I tried to expand it
to use named elements, which you can find in the 3rd (non-working) attempt below.

The error I'm getting is that "TypeError: rec is not a constructor"
I've tried some variations on the code, but without success.

Could you tell me the correct syntax to use to make it a constructor?
Thank you for any time invested into my question.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> Untitled </title>
<script type="text/javascript">

</script>

<style type="text/css">

</style>
</head>
<body>

<script type="text/javascript">
// Modified from: http://www.codingforums.com/showthread.php?t=286561

var s = "id1,name1,id2,name2,id3,name3...."
var temp = s.split(",");
var element = [ ];
for ( var t=0, e=0; t<temp.length; t+=2, ++e ) { element[e] = [ temp[t], temp[t+1] ]; }
alert(element.join('\n'));

// another test with 3 elements per record
var s = "id1,name1,phone1,id2,name2,phone2,id3,name3,phone3"
var temp = s.split(",");
var element = [ ];
for ( var e=0, t=0; t<temp.length; t+=3, ++e ) { element[e] = [ temp[t], temp[t+1], temp[t+2] ]; }
alert(element.join('\n'));

// still another test using DOM to define 4 elements per record 
// this version does NOT work correctly
var rec = { id:'', name:'', state:'', phone:'' };
var s = "id1,name1,state1,phone1,id2,name2,state2,phone2,id3,name3,state3,phone3"
var tarr = s.split(",");
var elem = [ ];
for ( var e=0, t=0; t<temp.length; t+=4, ++e ) {
  elem[e] = new rec;  // error seems to point to this line.
  elem[e].id = tarr[t];
  elem[e].name=tarr[t+1]
  elem[e].state=tarr[t+2];
  elem[e].phone=tarr[t+3];
}
var str = '';
for (var i=0; i<element.length; i++) {
  str += elem[i].id+','+elem[i]+','+elem[i].state+','+elem[i].phone+'<br>';
} document.write(str);

</script>

</body>
</html>
jmrker is offline   Reply With Quote