locked
Javascript Get ASP Element by ID RRS feed

  • Question

  • User-1821657828 posted
    I came up with this because i have been frustrated with trying to access ASP server objects through Javascript code. Because ASP changes the ID for ASP controls in many cases, the ID you use is not the one you access through Javascript. You can use server side code (<%= ASPControl.ClientID %>), but it makes it more difficult to make reusable code doing this and i've had trouble with linking javascript with server-side code. So i've piggy-backed a little on the MS AJAX $get(element) command and made the following script:

    function getASPElm(nm,tag) {
         if ($get(nm)) { return $get(nm); }
         if (!tag) { var tag = '*'; }
         var e = document.getElementsByTagName(tag);
         for (var i=0; i < e.length; i++) {
              if (e[i].id) {
                   if (e[i].id.indexOf(nm) != -1) {
                   return e[i];
                   }
              }
         }
    return null;
    }

    First it looks to see if it finds the element using the MS AJAX $get function. If not, if you included an HTML tag it will get a list of objects with that tag, if not it will get a list of all objects. Then it will cycle through them until it finds the item you specified. It's not perfect - but it works well for me. I bet someone could come up with something better though...


    Friday, May 4, 2007 11:13 PM

All replies

  • User1335583151 posted

    The use of master pages can make finding controls rather tricky when using JavaScript.  Does it work if you use it on a page that uses a master page?  Also, does it work in other browsers?  If so, well done! 

     

    Saturday, May 5, 2007 4:03 PM
  • User-1821657828 posted
    I've tried it in IE7, Firefox, & Opera and it works in each. And yes - it works with master pages. In fact, the reason i wrote it is because adding a master-page to a site i was working on broke some of my javascript and that's when i noticed the controls being renamed. [:)]
    Monday, May 7, 2007 10:11 PM
  • User-154051931 posted

    I dont think this will be 100% effective.

    What if you have somewhat similiar IDs, like txtName and txtNameOfDog?

    Tuesday, May 8, 2007 5:10 PM
  • User-1821657828 posted
    Yeah, i know it's not perfect. When i came across the problem, i looked around assuming that someone would already have a solution, but couldn't find one so i made my own. I suppose i could improve it by making sure the id ends with the text entered, but even then it could be faulty. It depends a bit on the programmers naming conventions as wel...
    Tuesday, May 8, 2007 8:27 PM
  • User1650543486 posted

    Ok, I don't know if this is the case but...

     

    If you're just calling a javascript function and need an ID of the field, just send this.id. You'll get the right id for javascript side of code, no matter what ID is on server side.

     

    Well, if this is the case...

    Thursday, August 21, 2008 5:55 AM