You can't locate objects by id strings (unless you are in a context where you could just use the id instead of eval anyways). If you want to pass around a reference to an object, you can do "var theCheckBox = english_weight1" somewhere where the english_weight1 id is accessible. Then you can pass it to where-ever you need it and use it like "if (theCheckBox.checked) //do something"
-- Alan Alpert On Mon, Apr 14, 2014 at 9:50 PM, Eric Feigenson <[email protected]> wrote: > Hello all... > > I'm writing some JavaScript code inside QML. What I'd like to do is be able > to access an object via id where the id is in a JavaScript string. > > For example, given the QML: > > CheckBox { > > id: english_weight1 > > text: qsTr("2.5 lb") > > } > > > and the JavaScript fragment: > > var idName = "english_weight1"; > > if (idName.checked) > > // do something is the box is checked... > > > But I want the idName in the "if" statement to refer to the value of the > JavaScript variable "idName", in other words, > "english_weight1.checked". I believe I could use eval: > > if (eval(idname + ".checked")) ... > > > but everything I've read says eval is to be avoided. > > If it helps or makes any difference, the CheckBox is in a Column with id > myColumn. I was thinking something like > myColumn[idName] would do the trick, but I'm only guessing. > > I hope this is a clear enough description... please let me know if any more > details are needed. > > Any thoughts? > > Thanks! > > -Eric > > _______________________________________________ > Interest mailing list > [email protected] > http://lists.qt-project.org/mailman/listinfo/interest > _______________________________________________ Interest mailing list [email protected] http://lists.qt-project.org/mailman/listinfo/interest
