In the following test code, is there a way to see if an element of the array already exists? I want to initialize it if it does not and append to it if it does.
[code=php]
<html>
<head>
<title>Append Elements</title>
<script type=”text/javascript”>
var Info = new Array();
var cnt = 0;
function AddInfo(str) {
// how do I check to see if element of hash needs to be created or if it already exists
// if (Info[str] == ‘undefined’) { Info.push(str); // doesn’t work well
if (Info[str] != ”) { Info.push(str); }
Info[str] += ‘:’+cnt; cnt++;
// above almost works, but get undefined errors
}
function ShowInfo() {
document.getElementById(‘entry’).value = ”;
foreach (var e in Info) {
document.getElementById(‘InfoText’).value = e+Info[e];
}
}
</script>
</head>
<body>
<input id=”entry” value=””>
<button onClick=”AddInfo(this.value)”>Add</button>
<button onClick=”ShowInfo()”>Show</button>
<br />
<textarea id=”InfoText” cols=”30″ rows=”10″></textarea>
</body>
</html>
Is this a correct approach or should the
var Info = new Array();
instead be
var Info = new Object();
Any suggested changes welcomed! Thanks. ?