Results 1 to 4 of 4

Thread: Is there any way to select all items in an array without using a loop?

  1. #1
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default Is there any way to select all items in an array without using a loop?

    I want to go through all checkboxes (all inputs are checkboxes) on a page and uncheck them if they are checked. This is what I came up with:
    Code:
    checkboxes = document.getElementsByTagName('input');
    for (var index = 0; index < checkboxes.length; index++){
    	checkboxes[index].checked = false;
    }
    My first instinct was to just do one of these:
    Code:
    checkboxes = document.getElementsByTagName('input');
    checkboxes.checked = false;
    //or
    checkboxes[all].checked = false;
    That of course doesn't work, but is there any other way to do something to all items in an array without using loops? Thanks!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    My instincts tell me no. The "way" would involve still looping through each whether or not you look at it. You could write a function called "dotoarray()" something like array_map() in PHP, but I'm not sure how that would be helpful.
    There may be some trick, though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    After some looking around, I found two methods, forEach and map, that look like they might help here, but I've no idea how. I still have a lot to learn.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Isn't foreach like in PHP, then? And I expect map() is like array_map() in PHP. The effect with both though is that you are actually using a loop-- it's just not part of the code that you write. For processing speed, etc., I expect it would be the same.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Tags for this Thread

Bookmarks

Posting Permissions

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