FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

[*RESOLVED*]Please close for me

 


bladesage
Okay, I have an awesome book on javascript. It goes very in-depth in arrays, but can't answer one simple question:

Can you later add more variables to an array, without rewriting the whole thing?

I have 10 variables for storing data. I want to store the empty ones inside another variable using an array. My code looks like this:

Code:
<script type="text/javascript" language="javascript">

var store1;
var store2;
var store3;
var store4;
var store5;
var store6;
var store7;
var store8;
var store9;
var store10;

var empties = new Array(store1,store2,store3,store4,store5,store6,store7,store8,store9,store10);

</script>


I begin the code by declaring each variable, then storing them each inside another variable using the "Array()" attribute.

Now, as I slowly begin to use and empty these variables (store1,store2,...store10), I want to obviously remove each from the list as it's filled. Then as I empty them, I want to add them back to the array again.

I can't do either of these things. I'm not new to javascript, but I'm slightly new to arrays in general. I can obviously refer back to the variables/value stored inside the arrays:

Code:

<script type="text/javascript" language="javascript">

function restorevals()
{
document.getElementById("outbox1").value = empties[2];
document.getElementById("outbox2").value = empties[0];
}

</script>


This wouldn't do much, returning some empty values, but you get my point.

HELP!!!


Last edited by bladesage on Tue May 23, 2006 10:52 pm; edited 1 time in total
kv
You can keep adding elements to js array. Make sure you specify correct index. Thats all.

Refer this site. It is helpful.
http://www.w3schools.com/js/js_obj_array.asp
Aredon
Quote:
I begin the code by declaring each variable, then storing them each inside another variable using the "Array()" attribute.

Array is not an attrbiute. Array is a constructor but is also called an array object.
An attribute describes a tag.
Given the code:
Code:

<input type="button" value="click here" onclick="alert(&quot;hello world!&quot;)" style="color:red;background:white;">

The element (an alias for tag) is an INPUT tag. The element has 4 set attributes.
TYPE, VALUE, ONCLICK, and STYLE. TYPE is an attribute. VALUE is an attribute. ONCLICK counts as both an attribute and an event handler. STYLE is an attribute. COLOR is a property. "red" is a property value of COLOR. BACKGROUND is a propery. "white" is a propety value of BACKGROUND.

Given the peice of code
Code:

window.alert("hello world!")

window is an object and alert is a method.

Another example:
Code:

Math.round(34.3)

Math is an object and round is one of it's methods.


Code:

alert(Math.PI)

Math is an object and PI is a property.


I hope that helps explain the technical terms straight and clear.


If you are interested in mastering arrays, you should learn how to use each of the methods available for manipulating an array.
http://www.w3schools.com/jsref/jsref_obj_array.asp


Last edited by Aredon on Tue May 23, 2006 10:37 pm; edited 1 time in total
bladesage
Aredon wrote:
Quote:
I begin the code by declaring each variable, then storing them each inside another variable using the "Array()" attribute.

Array is not an attrbiute. Array is a constructor but is also called an array object.
An attribute describes a tag.
Given the code:
Code:

<input type="button" value="click here" onclick="alert(&quot;hello world!&quot;)" style="color:red;background:white;">

The element (an alias for tag) is an INPUT tag. The element has 4 set attributes.
TYPE, VALUE, ONCLICK, and STYLE. TYPE is an attribute. VALUE is an attribute. ONCLICK counts as both an attribute and an event handler. STYLE is an attribute. COLOR is a property. "red" is a property value of COLOR. BACKGROUND is a propery. "white" is a propety value of BACKGROUND.

Given the peice of code
Code:

window.alert("hello world!")

window is an object and alert is a method.

If you are interested in mastering arrays, you should learn how to use each the methods available for manipulatingan array.
http://www.w3schools.com/jsref/jsref_obj_array.asp


Dude, boo-frickin-hoo! So I make one mistake in typing, SO WHAT? That doesn't make me an idiot, and I know very much about html, xhtml, css, javascript, and now php, so don't try and treat me like a little preschooler.

I know very well what an attribute is, and an object, method, handler, array, and all that other stuff. And I wouldn't even dream of wasting time typing "window.alert()" when "alert()" works perfectly well. The only reason I have this topic in the first place, is because I need to know how to add/remove pieces of an array, not to learn what it is. And I know very well how to add to one, now I just need to know how to remove something from it.
Aredon
I only wrote it the long way to specific that window is an object.

Is this what you are looking for?
Code:

<script type="text/javascript">
var blah=["a","b","c","d","e"];
alert(blah);
alert(blah.splice(2,1));//remove "c"
alert(blah);
</script>
bladesage
Aredon wrote:
I only wrote it the long way to specific that window is an object.

Is this what you are looking for?
Code:

<script type="text/javascript">
var blah=["a","b","c","d","e"];
alert(blah);
alert(blah.splice(2,1));//remove "c"
alert(blah);
</script>


Yes, that could work. Thanks, and sorry for snapping at you. I realized that right after I closed the window after posting.
mathiaus
-close-
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.