Stavros_S Stavros_S - 15 days ago 4x
Javascript Question

Looping through textFields in a Nativescript view

I would like to loop through all the textFields in a view. I know I can get a single field the following way:

exports.loaded = function (args) {
var layout = args.object;
fnameInput = layout.getViewById("fname-input");

It doesn't seem that there is a getViewsByClassName method, and I'm not finding anything in the docs that point me to a method which returns an array of views or elements within a view. Could someone point me in the right direction?


I would recommend using Nathanael Anderson’s nativescript-dom plugin, as it has a getElementsByClassName() function that seems to be what you’re looking for here.

If you don’t want to incur the dependency of the full plugin, you can refer to Nathanael’s implementation of that method to get an idea of how to use the View class APIs to get at the elements you need. Specifically, note the eachDescendant() method that all Views have, as you’ll need to use that to walk the NativeScript user interface tree.