Skip to content

Instantly share code, notes, and snippets.

@pec1985
Created June 22, 2012 18:42
Show Gist options
  • Save pec1985/2974458 to your computer and use it in GitHub Desktop.
Save pec1985/2974458 to your computer and use it in GitHub Desktop.
Titanium getElemetByTag()
/**
* Gets a Titanium Element
* @param {Ti.UI.View} Parent View - Where the view is
* @param {String} tag Property of the view
* @param {String} value Value of the custom property
* @return {Ti.UI.View} Return a view if found, otherwise null;
*/
function getElementByTag(parent, tag, value){
// Store the array in a variable for better performance
var childrenArray = parent.children;
// iterate through all the children
for(var i = 0, len = childrenArray.length; i < len; i++){
// Store the child in a variable for better performance
var child = childrenArray[i];
// get the custom property
var customProp = child[tag];
// Two checks
// 1. That the prop exists
// 2. If so, that the prop is equal to the value
if(customProp && customProp == value){
// return child view
return child;
}
}
// nothing found!
return null;
}
var view = Ti.UI.createView();
for(var i = 0, i < 20; i++){
view.add(
Ti.UI.createTextField({
value: 'foo',
bar: 'id'+i
})
);
}
var textField = getElementByTag(view, 'bar', 'id2');
if(textField != null){
// Do whatever
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment