Search code examples
javascriptnativescript

How do i get textfield value in nativescript


i'm trying to get the text from my textfield in nativescript core.

let textField = args.object;
const yourSearch = textField.getViewById("requiredSkill").text;

the error i get is : cannot read property 'text' of undefined

please help


Solution

  • NativeScript View XML TextView

    <TextView editable="true" id="requiredSkill"></TextView>
    

    A simple button with tap event

    <Button text="Button" tap="{{ onButtonTap }}" />
    

    The JavaScript code behind for that tap event

    var viewModel = observableModule.fromObject({
       onButtonTap: function (btargs) {
         let srcButton = btargs.object;
         const yourSearch =  srcButton.page.getViewById("requiredSkill").text;
       }
    });
    

    getViewById should be used on the <Page> model to search the entire page
    or you must have a container/layout which should have an id, first find the layout using getViewById then again do another getViewById within that layout to find the TextView

    so to answer your question, if it is a button click event, get the button and get the page of the button and find the view by Id

    srcButton.page.getViewById