SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to count number of hiddenfield on a page?

    Hi,
    I dynamically create hidden fields on a page, so I am unsure how many are on a particular page, how can I count the number of hiddn fields on a particular parge using Javascript? The hidden field are called hidden1, hidden2, hidden3 etc
    thanks a lot

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Here's a quick one
    Code:
    <script type="text/javascript">
    function getHiddenInputs() {
    //set up a variable to hold number of hidden inputs.
    var x = 0;
    //get all input tags
    var i = document.getElementsByTagName("INPUT");
    //loop through all input tags
    for (var j = 0; j < i.length; j++) {
      //find hidden inputs
      if (i.item(j).getAttribute("type") == 'hidden') {
      //if input is hidden, add to x
        x++;
      }
    }
    return x;
    }
    </script>

  3. #3
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to make this case insensitive, just in case.
    Code:
    if (i.item(j).getAttribute("type").toLowerCase() == 'hidden')
    You never know.
    Kiffin
    Your average future-famous kind of guy...

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by kgish
    You might want to make this case insensitive, just in case.
    Code:
    if (i.item(j).getAttribute("type").toLowerCase() == 'hidden')
    You never know.
    Good catch. I've written in XHTML so long that I almost forgot people can uppercase tags and attributes .

  5. #5
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good programming is just like good old defensive driving: always keep your eyes peeled for that crazy driver who just might happen to shoot out into the intersection from out of nowhere.

    My experience with teams of software developers has taught me that when experienced programmers think they are getting really experienced, hey I know the stuff just call me senior alright, they tend to overlook the obvious often enough -- which means that in the end good testers are not to be underestimated.
    Kiffin
    Your average future-famous kind of guy...

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hi

    thanks a lot,
    one quick thing,
    you know when you type 'return x' at the end of the code,
    what does that actually do? I mean does it send the value of x back to the whatever called it? cos you can still use the value of x without this cant you?
    thanks

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by damonps2003
    thanks a lot,
    one quick thing,
    you know when you type 'return x' at the end of the code,
    what does that actually do? I mean does it send the value of x back to the whatever called it? cos you can still use the value of x without this cant you?
    thanks
    Well, when you call the function, such as:
    Code:
    var numHiddens = getHiddenInputs();
    getHiddenInputs() is actually going to equal x, since that's the return value of the function. The variable x only exists when the function runs; you have to assign getHiddenInputs() to a global JS variable (or a variable in another function) to persist the value.

    Hope this made some kind of sense .

  8. #8
    SitePoint Zealot tss68nl's Avatar
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not give all hidden fields the same ID, and then get the collection of those directly?

    Haven't tried, so there might be some catch (for example, is that even possible in netscape...or...do you need them to have different ID's for something else?). However, I dont think you need them to have different ID's, since you can always give them a different name="" tag

    Going about things that way might be a little lighter on the iterations finding the correct elements.

  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by tss68nl
    Why not give all hidden fields the same ID, and then get the collection of those directly?
    Because only one element per page is supposed to have an ID. That's why it's called "id": because it's unique!

  10. #10
    SitePoint Zealot tss68nl's Avatar
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still you can label multiple elements with the same ID, it works in all browsers and you can retrieve them all as well. Do you always cross the street only on zebra crossings as well?

  11. #11
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by tss68nl
    Still you can label multiple elements with the same ID, it works in all browsers and you can retrieve them all as well. Do you always cross the street only on zebra crossings as well?
    Okay, you try to do anything else with that ID in a script and watch how fast it chokes. If you want to assign the same value to multiple elements, a class is definitely more suited to it. But hey, if you're searching by class then, you may as well search for "type=hidden" instead of class or ID.
    Off Topic:

    I don't write code that relies on browser quirks; you never know when the next version of a browser will "fix" those quirks .

  12. #12
    SitePoint Zealot tss68nl's Avatar
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually wrote multiple scripts that use the same id's for multiple elements out of speed considerations. And no, they dont choke at all, on any browser.

    For example, try a table with different stylesets on each other row, then with a mouseover highlight for the rows according to those styles, with the ability to select/deselect rows graphically. Ofcourse, you can do it the all neat way, but you either end up with *loads* of HTML code (which you really want to keep small out of download/rendering time), or a script that has to check too many elements to find what it's searching for, hence throwing away processing power/speed of the script. The reason for this is that the classes are already in use for something else...so one of the systems has to work on the ID, or otherwise you'd go the way of expando properties which is taking way too much resources to check.

    You try running bloated scripts on a pentium 2 and see how slow it runs

    Equally off topic :
    I dont believe in programming as an exact science. There is always a reason to consider doing things another way. The choice is ultimately up to the programmer, but any programmer thinking there is only one way to go about things is a bad one.


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
  •