The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

How to find 2 checkboxes on a web page

Last post 01-11-2008 6:34 AM by dhromed. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 01-09-2008 5:10 PM

    How to find 2 checkboxes on a web page

    That's easy... just look at EVERY element on the page and only do something if it's a checkbox.  Heck, there's only 5000+ elements, how long could that possibly take? 

    for (iCount=0;iCount<=document.all.length;iCount++) {
        elm = document.all[iCount];
        if (elm.type == 'checkbox') {
            // snip
        }
    }

    I program on the Internets
    Filed under:
  • 01-10-2008 4:20 AM In reply to

    • JvdL
    • Top 200 Contributor
    • Joined on 01-26-2007
    • Spain
    • Posts 151

    Re: How to find 2 checkboxes on a web page

    chortlehoort:
    Heck, there's only 5000+ elements, how long could that possibly take?

    About half a millisecond 

  • 01-10-2008 5:47 AM In reply to

    Re: How to find 2 checkboxes on a web page

    Not at the youtube 0.1 thread.
  • 01-10-2008 6:36 AM In reply to

    • JvdL
    • Top 200 Contributor
    • Joined on 01-26-2007
    • Spain
    • Posts 151

    Re: How to find 2 checkboxes on a web page

    To put it more clearly, the browser will iterate the 5000 elements many times over, doing CPU intensive stuff on each: load them, parse them, render them, resize them, ... One extra pass of 5000 string comparisons won't make noticeable difference.

    That doesn't mean the code snippet isn't a WTF : using document.getElementsByTagName("input") instead of document.all (which is IE-script, not JavaScript) would be a good start to improve it

  • 01-10-2008 7:14 PM In reply to

    • kaamoss
    • Top 500 Contributor
    • Joined on 11-01-2006
    • Irvine Ca
    • Posts 69

    Re: How to find 2 checkboxes on a web page

    Using IDs for accessing dom elements via javascript is usually the route I go. If I remember correctly, getting a dom element by  document.getElementById('id') is  typically faster than  document.getElementsByTagName("input"),  and returns one object, instead of an array of them.  $0.02 deposited...
  • 01-11-2008 3:41 AM In reply to

    • JvdL
    • Top 200 Contributor
    • Joined on 01-26-2007
    • Spain
    • Posts 151

    Re: How to find 2 checkboxes on a web page

    kaamoss:
    Using IDs for accessing dom elements via javascript is usually the route I go.

    True, but there are cases where you have no knowledge about the HTML, its elements and their id, if any, in particular when you make generic JS.

  • 01-11-2008 5:22 AM In reply to

    • kaamoss
    • Top 500 Contributor
    • Joined on 11-01-2006
    • Irvine Ca
    • Posts 69

    Re: How to find 2 checkboxes on a web page

    JvdL:

    kaamoss:
    Using IDs for accessing dom elements via javascript is usually the route I go.

    True, but there are cases where you have no knowledge about the HTML, its elements and their id, if any, in particular when you make generic JS.

    That's a good point, I guess I'm so used to not reusing that type of javascript code becase I can type it in emacs faster than I can think what I want it to do. For my code reuse, I try to pull out useful code into custom moo tool classes. Moo tools also gives a short_cut/cross browser way of accessing any dom element you want. I personally think that jQuery is better for this, but moo tools animations/ajax classes just seem to work better for me. If you're consistently writing js you're shooting yourself in the foot not leveraging some kind of js framework. 

  • 01-11-2008 6:34 AM In reply to

    Re: How to find 2 checkboxes on a web page

    This page has only 600+ elements. The average corp site -- that I build anyway -- has in the range of 100-200.

    What kind of utterly insane page has five thousand elements?

    (not including the Youtube 0.1 thread, which is accepted as being INSANE YAY)

    — Flurp.
Page 1 of 1 (8 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems