CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2006
    Posts
    306

    [RESOLVED] Try catch for checking element

    Okay, so here's the deal. I have a function that runs through a for loop, and picks elements and adds them to an array. I don't want to add an element that's not there, so I want to set up a try catch so that it can catch an error and hopefully return when it's trying to get an id that's not there.

    How do I check if the generated element id is valid?
    Or is there someway I can get the last child in a node, regardless of id?

    Code:
    <div id="mainDiv">
        <div id="subDiv1"></div>
        <div id="subDiv2"></div>
        <div id="subDiv3"></div>
        <div id="subDiv4"></div>
        <div id="subDiv5"></div>
    </div>
    Here is some sample code.

    HTML Code:
    function getElements()
    {
        var elemArray = new array();
        for (var i = 1; i < 9; i++)
        {
            var elem = document.getElementById("subDiv" + i);
            elemArray[i - 1] = elem;
        }
    }
    Last edited by code?; August 9th, 2008 at 02:40 AM.

  2. #2
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Try catch for checking element

    Here's the easiest way:
    Code:
    if (elem = document.getElementById("subDiv" + i) {
    Please note that it uses =, not ==
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  3. #3
    Join Date
    May 2006
    Posts
    306

    Re: Try catch for checking element

    Alright, I'll try that real quick.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured