-1
I am creating a hybrid app with Cordova, and I want the user to log in first, it stays logged in direct. If it leaves the app for example, and logs in again, it doesn’t need to log in. I am using the code below so far, the value is stored in Localstorage, more is not displayed in the input after updating the same.
index.html
<h1 style="font-size: 1.8em;">Login</h1>
<div class="alert alert-danger text-center" id="message"></div>
<div>
<input type="name" class="form-control" id="txt_username" name="txt_username" placeholder="Usuário">
</div>
<div>
<input type="password" class="form-control mt-2" id="txt_pwd" name="txt_pwd" placeholder="Senha"/>
</div>
<div>
<input type="button" value="Entrar" class="btn btn-danger btn-block mt-3" name="but_submit" id="but_submit" />
</div>
index js.
var login = document.getElementById('txt_username');
login.addEventListener('input', (e)=>{
window.localStorage.setItem('login', login.value);
var cat = window.localStorage.getItem('login');
$("#txt_username").val(cat);
})
The
document.getElementById('txt_username').value();
will take the value of the element at the time it was executed. If the page was loaded this instant and the#txt_username
has an empty value, the empty value will be saved. Use the own to inspect element and see in Localstorage the saved value, even in mobile is possible to do this.– Inkeliz
How would you get the value at the time it is typed?
– André Santos
Still the same problem, I did the following test
var login = "Olá"
; and he displays normally.– André Santos
@Andrésantos can do this using the
addEventListener
, there is the event ofchange
and ofinput
. Then, when typed/modified you store the value.– Inkeliz
I’m sorry but I never used addeventlistener, could you give me an example of what my code would look like with it?
– André Santos
@Augustovasques understand more I intend to pass later to the sqlite. For now the Localstorage would break a branch.
– André Santos
A didn’t work :( I added an image the question of how my final code was, but it didn’t work
– André Santos
If I take
value
make a mistake[object HTMLInputElement]
– André Santos
There’s no saving it anyway
– André Santos
Edit the question and put a [mcve] containing the html and code that is trying to use localStorage.
– Augusto Vasques
I don’t understand, it’s not storing in Localstorage the value, more like I said if I do
var login = "Olá"
it stores normally– André Santos
I modified the question, added the suggestions and HTML
– André Santos
The code works perfectly. See: https://repl.it/EquatorialExtrasmallCircles the problem is that you are trying to update an input with a value it already contains and the change is visually redundant.
– Augusto Vasques
Yes it takes the value, plus when refreshing the page the value is not displayed in the input
– André Santos
But you didn’t program for this!!! Damn, you have to put this functionality to recover the data from localStorage in the event
load
page. In the eventinput
you store the data onload
you recover.– Augusto Vasques
@Augustovasques I updated the question when you asked, and I put this detail :)
– André Santos