Click to See Complete Forum and Search --> : Output Array to textarea?


fiedler
04-04-2003, 04:00 PM
I only use Javascript occassionally, so please bear with me.

I have a web page where customers can look up a distributor by entering a postal code.
An array contains company, address, location, phone, and web for each distributor.

I haven't been able to figure out a nice way to display the results on my web page so that there is a line break between each data field. The results come out as one string separated by commas. Where do the commas come from??

I've tried popup windows, textarea boxes, buttons, etc., but html is ignored and I cant't get rid of the commas and put line breaks in. I'd like to avoid a popup window, due to popup killers. With some examples I've used to write the results nothing appears.
Any ideas?
Thanks much!!

khalidali63
04-04-2003, 04:13 PM
Sounds like you are getting values from a database?..
If so then you may oonly need to add <br/>
tags where its adding a comma

Cheers

Khalid

Jona
04-04-2003, 04:16 PM
Are you, by any chance, using Javascript split() method? If so, that's your problem. You always have to use join() afterwards. Just in case.

Anyways, you can use <br/> or \n (<br/> should work, though). You can, though, just use stringVar.split(',').join('<br>'); or something like that.

fiedler
04-04-2003, 04:17 PM
No, the data values are in the JS program as array elements..
Tried <br> tags, but they show up as <br>

Another question: Can I assign all the values of one array to another by simpy using the "=" arithmetic operator?

Jona
04-04-2003, 04:21 PM
Another question: Can I assign all the values of one array to another by simpy using the "=" arithmetic operator?

Yup, sure can. ;)

As for your question, may we see the Javascript code you're dealing with?

Nedals
04-04-2003, 04:23 PM
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">

<html><head><title>Untitled</title>
<script type="text/javascript">
<!--
data = new Array('name','address','city','state');
//******* you probably have something like this.
//function setarea () { document.forms[0].elements[0].value = data; }

//******* Change to this
function setarea() {
newdata = data.join('\n');
document.forms[0].elements[0].value = newdata;
}
onload = setarea;
//-->
</script>
</head>

<body bgcolor="#ffffff" topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
<form>
<textarea cols=50 rows=20></textarea>
</form>
</body>
</html>

fiedler
04-04-2003, 04:23 PM
What I mean does the statement arrayX = arrayZ
assign the values of arrayZ to arrayX?

Jona
04-04-2003, 04:30 PM
Maybe a for() loop:

var arX = new Array('a','b','c','d','e');
var arZ = new Array('f','g','h','i','j');

for(p=0;p<5;p++){arX[p] = arZ[p];
alert(arX[p]);}

Your alert should be: "a" then another alert, "b" then another, "c" and so on.

fiedler
04-04-2003, 04:34 PM
A snipet of my code (please don't laugh to loud)

function Zipfind(zip)
{
var repA = new Array(6);
var AEI = new Array(6);
AEI[0] = 'AEI Corp';
AEI[1] = 'American Way';
AEI[2] = 'Detroit, MI ';
AEI[3] = 'Phone: 555-0315';
AEI[4] = 'Fax: 555-7502';
AEI[5] = 'www.aeicorp.com';
.
.
.
if (zip >= 48000 && zip <=49999 )
{document.Form.rep.value = AEI;
return (false);

"rep" is a textarea.
Is there a better way to write the results?

Thanks nedals!!!

It's that "/n" that fixed it. I must have spaced out the day the teacher mentioned that one.

fiedler
04-04-2003, 04:46 PM
my write statement becomes:

if (zip >= 48000 && zip <=49999 )
{document.Form.repA.value = AEI[0]+'\n'+AEI[1]+'\n'+AEI[2]+'\n'+AEI[3]+'\n'+AEI[4]+'\n'+AEI[5];
return (false);
}


Thanks again!!

fiedler
04-04-2003, 04:56 PM
Next question....

Any way to make a website link in a text area??

Nedals
04-04-2003, 05:06 PM
// OR ***********
function Zipfind(zip) {
var repA = new Array(6);
// Simplified method of setting an array
var AEI = new Array(
'AEI Corp',
'American Way',
'Detroit, MI ',
'Phone: 555-0315',
'Fax: 555-7502',
'www.aeicorp.com'
);

// I highly recommend you do not use the name 'form' for a form name. If you start using unnamed forms
// you could well find yourself writing 'form[0]' instead of 'forms[0]'
if (zip >= 48000 && zip <=49999 )
document.theform.rep.value = AEI.join("\n"); // using the features of javascript
return (false);
}

fiedler
04-04-2003, 05:22 PM
Thanks,..
I knew about the short way of arrays, but went back to the standard way when I was pulling my hair out. You know, back to basics when all else fails.

I think I need a good book on JavaScript. For example, I didn't know about the "join" method.

Thanks for your help!!