Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8638365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:42:39+00:00 2026-06-12T10:42:39+00:00

Method UiInstance.getElementById(ID) always returns GenericWidget object, even if ID does not exist. Is there

  • 0

Method UiInstance.getElementById(ID) always returns GenericWidget object, even if ID does not exist.

Is there some way how to find out that returned object does not exist in my app, or check whether UI contains object with given ID?


Solution for UI created with GUI builder:

function getSafeElement(app, txtID) {
    var elem = app.getElementById(txtID);
    var bExists = elem != null && Object.keys(elem).length < 100;
    return bExists ? elem : null;
}

It returns null if ID does not exist. I didn’t test all widgets for keys length boundary, so be careful and test it with your GUI.

EDIT: This solution works only within doGet() function. It does not work in server handlers, so in this case use it in combination with @corey-g answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:42:40+00:00Added an answer on June 12, 2026 at 10:42 am

    This will only work in the same execution that you created the widget in, and not in a subsequent event handler where you retrieve the widget, because in that case everything is a GenericWidget whether or not it exists.

    You can see for yourself that the solution fails:

    function doGet() {
      var app = UiApp.createApplication();
      app.add(app.createButton().setId("control").addClickHandler(
          app.createServerHandler("clicked")));
      app.add(app.createLabel(exists(app)));
      return app;
    }
    
    function clicked() {
      var app = UiApp.getActiveApplication();
      app.add(app.createLabel(exists(app)));
      return app;
    }
    
    function exists(app) {
      var control = app.getElementById("control");
      return control != null && Object.keys(control).length < 100;
    }
    

    The app will first print ‘true’, but on the click handler it will print ‘false’ for the same widget.

    This is by design; a GenericWidget is a “pointer” of sorts to a widget in the browser. We don’t keep track of what widgets you have created, to reduce data transfer and latency between the browser and your script (otherwise we’d have to send up a long list of what widgets exist on every event handler). You are supposed to keep track of what you’ve created and only “ask” for widgets that you already know exist (and that you already know the “real” type of).

    If you really want to keep track of what widgets exist, you have two main options. The first is to log entries into ScriptDb as you create widgets, and then look them up afterwards. Something like this:

    function doGet() {
      var app = UiApp.createApplication();
      var db = ScriptDb.getMyDb();
      // You'd need to clear out old entries here... ignoring that for now
      app.add(app.createButton().setId('foo')
          .addClickHandler(app.createServerHandler("clicked")));
      db.save({id: 'foo', type: 'button'});
      app.add(app.createButton().setId('bar'));
      db.save({id: 'bar', type: 'button'});
      return app
    }
    

    Then in a handler you can look up what’s there:

    function clicked() {
      var db = ScriptDb.getMyDb();
      var widgets = db.query({}); // all widgets
      var button = db.query({type: 'button'}); // all buttons
      var foo = db.query({id: 'foo'}); // widget with id foo
    }
    

    Alternatively, you can do this purely in UiApp by making use of tags

    function doGet() {
      var app = UiApp.createApplication();
      var root = app.createFlowPanel();  // need a root panel
      // tag just needs to exist; value is irrelevant.
      var button1 = app.createButton().setId('button1').setTag(""); 
      var button2 = app.createButton().setId('button2').setTag("");
      // Add root as a callback element to any server handler
      // that needs to know if widgets exist
      button1.addClickHandler(app.createServerHandler("clicked")
          .addCallbackElement(root));
      root.add(button1).add(button2);
      app.add(root);
      return app;
    }
    
    function clicked(e) {
      throw "\n" +
          "button1 " + (e.parameter["button1_tag"] === "") + "\n" + 
          "button2 " + (e.parameter["button2_tag"] === "") + "\n" + 
          "button3 " + (e.parameter["button3_tag"] === "");
    }
    

    This will throw:

    button1 true
    button2 true
    button3 false
    

    because buttons 1 and 2 exist but 3 doesn’t. You can get fancier by storing the type in the tag, but this suffices to check for widget existence. It works because all children of the root get added as callback elements, and the tags for all callback elements are sent up with the handler. Note that this is as expensive as it sounds and for an app with a huge amount of widgets could potentially impact performance, although it’s probably ok in many cases especially if you only add the root as a callback element to handlers that actually need to verify the existence of arbitrary widgets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Method alive(port) in RemoteActor does not take IP address as parameter. It constructs internally
Method chaining is the practice of object methods returning the object itself in order
Method keySet() . Examples works fine, but I not sure if I'm right.
Which method should I override to be guaranteed to always get the correct size
Method 'Private Sub trigger_Triggered(sender As Object, e As OamsIDD.VoamsWorldIDD.OamsStateChangeEventArgs)' cannot handle event 'Public Event
Inside Method A, there is method B. Method B throws exception, but method A
The method Object.defineProperty (see here ) accepts as third argument a descriptor. What is
1 - Method Chaining I really love the way you can call functions without
A method have some methods and also some methods have other methods. How should
My method accepts a Method object as a parameter. Therefore, I need to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.