Search code examples
dynamictitaniumtextinput

Titanium: Unable to access dynamically added TextInput's value on click of a button


index.xml

<Alloy>
    <Window class="container">
    </Window>
</Alloy>

index.js

function btnClickedHandler(e){
    alert(nameTi.text);
    alert($.index.nameTi);
}
function addContent(){
    var showBtn = Ti.UI.createButton({
        id: "showButton",
        title:"Show Text",
        top: 10,
    });
    var nameTi = Ti.UI.createTextField({
        id:"displayText",
        textFieldId: 'displayText',
        width: 50
    });
    showBtn.addEventListener("click", btnClickedHandler);
    $.index.add(nameTi);
    $.index.add(showBtn);
}
$.index.open();
addContent();

Any suggestions to access dynamically added text input. One way could be definitely by storing instance in global variable. But can be done using id like $("#instanceId")


Solution

  • Try this:

    function addContent(){
        var showBtn = Ti.UI.createButton({
            id: "showButton",
            title:"Show Text",
            top: 10,
        });
        var nameTi = Ti.UI.createTextField({
            id:"displayText",
            textFieldId: 'displayText',
            width: 50
        });
        showBtn.addEventListener("click", function(){
        alert(nameTi.text);
        alert($.index.nameTi);
       }
    });
        $.index.add(nameTi);
        $.index.add(showBtn);
    }
    $.index.open();
    addContent();