/    Sign up×
Community /Pin to ProfileBookmark

Clearing an Array

Anyone know how to clear an array using the onclick function?

Every time I hit a “submit” button, items are saved into an empty array called boxes and displayed on the screen. In order to clear the items from the screen, i hit the “clear button, the items clear from the screen, but not from the array.

I have a function named “clear” where i delete the items from the screen and
i’ve tried using boxes.length = 0 or Array.clear(boxes) = []; and those didn’t work (notice i use those inside the function named “clear”

Any suggestions??

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 18.2012 — Assuming the array is named 'boxes', try ...
<i>
</i> boxes = [];


Alternatively, you could try...
<i>
</i> &lt;button onclick="boxes=[]"&gt;Clear Boxes&lt;/button&gt;
Copy linkTweet thisAlerts:
@pholderauthorAug 18.2012 — This still isn't clearing the array, I'm still getting counter keeps going even after i hit the clear button,

check it out:

http://pholder.userworld.com/javascript1/javascript1homeowork/boxes.html
Copy linkTweet thisAlerts:
@JMRKERAug 18.2012 — I'm not sure I'm seeing the problem.

The display is cleared.

What counter is not resetting?

?
Copy linkTweet thisAlerts:
@pholderauthorAug 18.2012 — Try clicking the submit button, then click on a box and check out the id number. After that, hit the clear button, all boxes should clear.

Hit the submit button and click on a box again and check out the id number. It's supposed to start over from zero, but instead the id number keeps going from the previous.

Every time the clear button is hit, the array should be cleared.

You know what I mean?
Copy linkTweet thisAlerts:
@JMRKERAug 18.2012 — Have you tried ...
<i>
</i>//Clears the boxes <br/>
function clear() {
var sceneDivs = document.querySelectorAll("div#scene div");
for (var i = 0; i &lt; sceneDivs.length; i++) {
var scene = document.getElementById("scene");
var clear = document.getElementsByTagName("div")[1];
scene.removeChild(clear);
}
boxes.length = 0; <br/>
[COLOR="Red"]counter = 0;[/COLOR] // or = 1;
}
Copy linkTweet thisAlerts:
@pholderauthorAug 18.2012 — Thank you for your response,

I tried both of those and they didn't work.
Copy linkTweet thisAlerts:
@JMRKERAug 19.2012 — Try clicking the submit button, then click on a box and check out the id number. After that, hit the clear button, all boxes should clear.

Hit the submit button and click on a box again and check out the id number. It's supposed to start over from zero, but instead the id number keeps going from the previous.

Every time the clear button is hit, the array should be cleared.

You know what I mean?[/QUOTE]


Thank you for your response,

I tried both of those and they didn't work.[/QUOTE]


Following your directions from the earlier post, I don't see the problem.

The ID values do seem to reset to 0 in my testing. ?
Copy linkTweet thisAlerts:
@ToftebachAug 25.2012 — I just visited the above URL in IE8 and IE9. No Boxes were shown.

Your JavaScript contains some statements such as
<i>
</i>console.log(counter);

It seems console.log is not supported in IE (error message: "console" is not defined).

I tried to comment those statements out. Then Boxes could be created and cleared in IE9 - just as in Firefox and Chrome.

You might want to do the same.

Good luck!
×

Success!

Help @pholder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...