How to insert text into input fields using js?

Issue

I am working on ionic framework. I want to know how to insert text into input label from local storage. I want to add text in username input box if data is stored locally .Here goes the code.

In login.html

<label class="item item-input item-stacked-label">
   <span class="input-label">Username</span>
   <input type="text" ng-model="xxx.user" id="userInput"  placeholder="Email or Phone">
</label>

In controller .js

var loadData1 =window.localStorage.getItem("data");
console.log(loadData1);
    var userInput=document.getElementById("userInput");
   if (window.localStorage.getItem("data") !== null){
        userInput.innerHTML =loadData1;
            console.log(window.localStorage.getItem("data"));
     };
   

Solution

add the data to the model of the input.

for ex:

$scope.xxx = {'user': '' , 'password': ''};

var loadData1 =window.localStorage.getItem("data");
console.log(loadData1);

if (loadData1 != null){
   $scope.xxx.user = loadData1;
   console.log( $scope.xxx.user);
};

Answered By – Sajal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published