if you have no objection to me rewriting your code, this is how I would do it, with comments in code:
Code:
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object with attributes
person = {
giv:firstname,
sur:lastname
}
//push object onto array
personarr.push(person);
//use innerHTML because document write is for suckers
document.getElementById("name").innerHTML=personarr[0].giv+" "+personarr[0].sur;
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='createperson("Djinn","Juice")'>
<div id="name"></div>
</body>
</html>
or (closer to what you were doing in the first place):
Code:
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object without attributes
person =firstname+" "+lastname;
//push object onto array
personarr.push(person);
//use innerHTML because document write is for suckers
document.getElementById("name").innerHTML=personarr[0];
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='createperson("Djinn","Juice")'>
<div id="name"></div>
</body>
</html>
I appreciate the help but the point of an array in an array is to be able to loop through the arrays. I'm not sure using objects would be optimal for what I wish to do.
ie:
Code:
for (i=0,i<=count(personarr),i++)
{
document.writeln(personarr[i];
}
which I can figure out how to do properly on my own if I can figure out how to add arrays to arrays in the way I have setup.
maybe if you explained a little more what it is you're trying to do?
Code:
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object without attributes
person =firstname+" "+lastname;
//push object onto array
personarr.push(person);
//use innerHTML because document write is for suckers
}
function showNames() {
for (var k=0; k<personarr.length; k++) {
document.getElementById("name").innerHTML+=personarr[k]+"<br>"
}
}
function makeNames() {
createperson("Djinn","Juice");
createperson("Harry","Potter");
createperson("Super","Man");
showNames();
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='makeNames()'>
<div id="name"></div>
</body>
</html>
What I'm trying to do is create a function that allows me to insert an array into an array, where the function variables are the ones inserted as an array
in PHP it would be like this
PHP Code:
//setup array variable
$people_array=array();
//setup function which creates an array within $people_array
function create_people($firstname,$lastname,$state)
{
global $people_array;
//loop through each item in $people_array and print every item one by one
for($x=0;$x<=count($people_array)-1;$x++)
{
for($y=0;$y<=count($people_array[$x])-1;$y++)
{
print $people_array[$x][$y]."<br />";
}
}
yeah, I think I see that and it is certainly possible although a little weird. Can I ask why you wouldn't use the first approach (accessing the object's attributes) or the last one (string the name and surname as one attribute and then looping through all of them)?
depending on what you actually want to do, I think a combination of the two would probably be most logical.
to address your second post in this thread, pretty much everything in javascript is an object (even functions can be objects) - it's a little hard to get away from them...
The reason I want to do this is to print XML with tags around each attribute such as Firstname, Lastname, City, and to do so I have to do it the way I have for an example.
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object with attributes
person = {
giv:firstname,
sur:lastname
}
//push object onto array
personarr.push(person);
}
function showNames() {
var names = document.getElementById("name");
for (var k=0; k<personarr.length; k++) {
var mydiv=document.createElement('div')
mydiv.appendChild(document.createTextNode('<some tag>'+personarr[k].giv+'</some tag> '+'<some other tag>'+personarr[k].sur+'</some other tag>'));
names.appendChild(mydiv);
}
}
function makeNames() {
createperson("Djinn","Juice");
createperson("Harry","Potter");
createperson("Super","Man");
showNames();
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='makeNames()'>
<div id="name"></div>
</body>
</html>
Bookmarks