www.webdeveloper.com
Results 1 to 9 of 9

Thread: Clearing an Array

  1. #1
    Join Date
    Jul 2012
    Posts
    28

    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??

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    Assuming the array is named 'boxes', try ...
    Code:
      boxes = [];
    Alternatively, you could try...
    Code:
      <button onclick="boxes=[]">Clear Boxes</button>

  3. #3
    Join Date
    Jul 2012
    Posts
    28
    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/javascr...ork/boxes.html

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Question

    I'm not sure I'm seeing the problem.
    The display is cleared.
    What counter is not resetting?

  5. #5
    Join Date
    Jul 2012
    Posts
    28
    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?

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    Have you tried ...
    Code:
    //Clears the boxes           
    function clear() {
      var sceneDivs = document.querySelectorAll("div#scene div");
      for (var i = 0; i < sceneDivs.length; i++) {
        var scene = document.getElementById("scene");
        var clear = document.getElementsByTagName("div")[1];
        scene.removeChild(clear);
      }
      boxes.length = 0;  
      counter = 0;  // or = 1;
    }

  7. #7
    Join Date
    Jul 2012
    Posts
    28
    Thank you for your response,

    I tried both of those and they didn't work.

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Question

    Quote Originally Posted by pholder View Post
    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 Originally Posted by pholder View Post
    Thank you for your response,

    I tried both of those and they didn't work.
    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.

  9. #9
    Join Date
    Aug 2012
    Posts
    3

    console.log does not work in Internet Explorer

    I just visited the above URL in IE8 and IE9. No Boxes were shown.

    Your JavaScript contains some statements such as
    Code:
    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!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles