View Full Version : Alert Contents of an Array
gillweb
06-29-2006, 01:19 PM
I'm looking to take an array like this
var nochecks = new Array()
nochecks[0] = "MY NAME"
nochecks[1] = "YOUR NAME"
nochecks[2] = "HIS NAME"
And when a link is clicked (OnClick) it will alert each line of the array (not seperate alerts just one alert). I would assume I could do this with sometype of loop but none of the code I have put together works. Can anyone help with this one?
coothead
06-29-2006, 02:54 PM
Hi there gillweb,
...none of the code I have put together works
Well, you had better try this then...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>list an array onclick</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
window.onload=function() {
var items='';
var nochecks = new Array();
nochecks[0]='MY NAME';
nochecks[1]='YOUR NAME';
nochecks[2]='HIS NAME';
document.getElementById('link').onclick=function() {
for(c=0;c<nochecks.length;c++) {
items+=nochecks[c]+'\n';
}
alert(items);
}
}
//-->
</script>
</head>
<body>
<div>
<a id="link" href="#">itemize the array</a>
</div>
</body>
</html>
gillweb
06-29-2006, 03:09 PM
Works Ok but when you click it multiple times (in FireFox anyway) it duplicates each line.
First click looks like this:
MY NAME
YOUR NAME
HIS NAME
Second click looks like this:
MY NAME
YOUR NAME
HIS NAME
MY NAME
YOUR NAME
HIS NAME
coothead
06-29-2006, 03:18 PM
Hi there gillweb,
sorry about that, I did not check for multiple clicks. :o :o :o
No problem, though. :agree:
The highlighted line will take care of it. ;)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>list an array onclick</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
window.onload=function() {
var items='';
var nochecks = new Array();
nochecks[0]='MY NAME';
nochecks[1]='YOUR NAME';
nochecks[2]='HIS NAME';
document.getElementById('link').onclick=function() {
for(c=0;c<nochecks.length;c++) {
items+=nochecks[c]+'\n';
}
alert(items);[color=purple]
items='';
}
}
//-->
</script>
</head>
<body>
<div>
<a id="link" href="#">itemize the array</a>
</div>
</body>
</html>[color]
Jon Hanlon
06-29-2006, 07:25 PM
alert(nochecks.join("\n"))
;)
coothead
06-30-2006, 01:51 AM
Hi Jon
alert(nochecks.join("\n"))
Right, that's it. I've done enough code bloat. :o :o :o
I think it's time for me to take up knitting.
@invasion@
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.