# Perspective DOM: getElementById and querySelector for Perspective scripts

**URL:** <https://forum.inductiveautomation.com/t/perspective-dom-getelementbyid-and-queryselector-for-perspective-scripts/117966>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [November 25, 2026, 12:56pm UTC](https://forum.inductiveautomation.com/t/perspective-dom-getelementbyid-and-queryselector-for-perspective-scripts/117966 "2026-11-25T12:56:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathanmoore4472](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@nathanmoore4472](https://forum.inductiveautomation.com/u/nathanmoore4472)\
**Post date:** [November 25, 2026, 12:56pm UTC](https://forum.inductiveautomation.com/t/perspective-dom-getelementbyid-and-queryselector-for-perspective-scripts/117966/1 "2026-11-25T12:56:04Z")

</div>

Hi all,

I've been working on a small module for Ignition 8.3 and wanted to share it, along with a few things I learned building it.

### The problem

Most of us have written something like this:

```python
self.parent.getChild("FlexContainer").getChild("FlexContainer").getChild("SaveButton").props.text

```

During development, layouts change constantly. Rename a container or move the button into a new flex and every script that walked that path needs rewiring. The lines are long, hard to read, and tied to the exact structure of the view.

### The idea: borrow from the web

In the browser you'd write `document.getElementById("saveButton")`. Perspective already has `meta.domId`, so the first version added exactly that:

```python
btn = system.dom.getElementById("saveButton")
btn.props.text = "Saved"

```

It returns the same kind of object as `self`, or `None` if nothing matches. It reads the current page and view from the script's thread, the same way the built-in `system.perspective` functions do, so you don't pass `self`. By default it only searches the view the script runs in. Searching docks, popups and embedded views takes a separate call, `system.dom.getPageElementById`, so leaving the view is always deliberate.

### What I found when I measured it

Perspective only lets a component's property tree be read on its execution queue. Script threads aren't on that queue, so every `meta.domId` check is a hand-off to the queue plus a wait, about 35–45 µs per component. A component's name and type are plain fields held in memory, so checking them is far cheaper.

Test views of 106–129 components, Ignition 8.3.9 in Docker, timed with `System.nanoTime()`, ranges over several runs. Rows marked \* include one `.props.text` read:

| Lookup | Time |
| --- | --- |
| Native `getChild` chain, 7 levels \* | 56–215 µs |
| `getElementByName` \* | 67–93 µs |
| `getElementById`, early hit \* | 0.3–0.6 ms |
| `getElementById`, deep hit \* | 1.8–2.5 ms |
| `getElementById`, miss | 3.2–4.3 ms |

That's imperceptible for a button click, but it adds up in scripts that run often (although you shouldn't be getting the reference fresh every time, if in a loop). So the module grew:

- `getElementByName` / `getElementsByName` match `meta.name` with no queue hops. Names survive moving a component but not renaming it; `domId` survives both.
- `querySelector` / `querySelectorAll` support a small CSS-like syntax: `button`, `#id`, `.class`, `[name="Save"]`, compounds like `button#save`, and comma unions. Cheap checks (type, name) run first, so `button#save` only reads `domId` on buttons.
- Multi-match versions return Python lists, and every function has a `getPage*` version for searching the whole page.

### The surprise: sometimes faster than native

On the benchmark suite view, `getElementByName("DeepTarget")` searched all 106 components and still matched or beat the hand-written chain to the same button in every run: 67–93 µs against 85–215 µs. That's the opposite of what I expected from a search versus a direct path.

The cost of a `getChild` chain isn't finding the child. In Java, `getChild` is a quick match on names held in memory. The cost is crossing between Jython and Java at every step. Each `.parent` or `.getChild(...)` in the script:

1. looks the attribute up on Perspective's script wrapper (which checks for custom methods first, then falls back to Jython's reflection),
2. calls into Java, and
3. wraps the returned component in a new script wrapper.

`self.parent.parent.getChild("Body").getChild("SecDeep")...` is about ten of those round trips. `getElementByName` is one: a single call into Java, an in-memory walk over the tree with plain string compares, and one wrapper for the result.

It isn't a guaranteed win. On the 129-component view the native chain ran in 56–71 µs, so treat name lookups as "about the same cost as the chain, without the brittleness" rather than an optimisation. And none of this applies to `domId` lookups, which pay a queue hop for every component checked.

### What I learned

- **Measure, and measure more than once.** My results varied by up to ±50% between runs, so a single number would have been misleading.
- **Property access costs too.** Every `.props.x` read or write, on `self` included, goes through that same queue (about 45 µs measured). Look a component up once per script and keep the reference, but don't expect the reads after that to be free.
- **Count the crossings, not just the work.** A search done entirely in Java can beat a short path that hops between Jython and Java at every step.
- **Fail loudly.** An early version of the selector parser silently ignored attributes it didn't understand, so `[enabled=true]` matched every component in the view. Unsupported syntax now raises an error.
- **`except Exception:` doesn't catch Java exceptions in Jython.** Use a bare `except:` or catch the specific Java class.
- **Make scope explicit.** A script should never reach into another view by accident.

### Try it:

Write up, live demo, source and a signed .modl that also runs (MIT, Ignition 8.3): [http://portfolio.mooresedge.co.uk/ign-dom](http://portfolio.mooresedge.co.uk/ign-dom)

It's early, so I'd really welcome feedback. In particular: is there a supported API for this that I've missed, and what other lookups would you find useful?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 25, 2026, 1:18pm UTC](https://forum.inductiveautomation.com/t/perspective-dom-getelementbyid-and-queryselector-for-perspective-scripts/117966/2 "2026-11-25T13:18:17Z")

</div>

> [@nathanmoore4472](#):
>
> During development, layouts change constantly. Rename a container or move the button into a new flex and every script that walked that path needs rewiring. The lines are long, hard to read, and tied to the exact structure of the view.

This is exactly why I create custom properties on the view itself then bind component values, etc., to those. You can change your view layout all you want, and it will not break anything with this design.

Credit @pturmel for prompting me to design this way.

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [November 25, 2026, 1:38pm UTC](https://forum.inductiveautomation.com/t/perspective-dom-getelementbyid-and-queryselector-for-perspective-scripts/117966/3 "2026-11-25T13:38:19Z")

</div>

I agree with @dkhayes117 that the best solution is to simply bind _upwards_.

IMO it’s also not really checking the DOM… but the gateway-side model of what’s _supposed_ to be in the DOM 🤷‍♂️ Does this traverse into embedded views/repeated views?

I do see how this could be useful though, but I think it’d be more accurate/generally useful as something like `system.perspective.findComponent(predicate)`, where the user could provide a predicate that checks for the ID they care about.

---

<div class="post-metadata">

**Author:** ![nathanmoore4472](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@nathanmoore4472](https://forum.inductiveautomation.com/u/nathanmoore4472)\
**Post date:** [November 25, 2026, 1:52pm UTC](https://forum.inductiveautomation.com/t/perspective-dom-getelementbyid-and-queryselector-for-perspective-scripts/117966/4 "2026-11-25T13:52:04Z")

</div>

Yeah it's not really the DOM, just received that name due the original idea stemming from web JavaScript.

It _Can_ traverse into embedded and repeated views with the 'page' versions of the functions `system.dom.getPageElementById(id)` but I decided not to make this the default behavior as this seems quite dangerous and I feel it could be very easy to accidentally select components unintentionally.

Agreed that binding upwards is the best way to design in ignition currently, but I do think there is some additional utility to be gained from the module for cleaner and more easy to read code.

simple example

```python
# Clear every text field in the current view
for field in system.dom.querySelectorAll("text-field"):

    field.props.text = ''

```

---

<div class="post-metadata">

**Author:** ![Steve\_Laubach](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steve_laubach/32/60900_2.png) [@Steve\_Laubach](https://forum.inductiveautomation.com/u/Steve_Laubach)\
**Post date:** [November 25, 2026, 3:46pm UTC](https://forum.inductiveautomation.com/t/perspective-dom-getelementbyid-and-queryselector-for-perspective-scripts/117966/5 "2026-11-25T15:46:16Z")

</div>

Yeah, I started doing that based on Phil's comments and it really dodges a lot of problems.
