JavaScript When going back and forth between pages it keeps input value but not text set by jQuery when going back to school,when going back to moon,

I have a page with an empty text input, an empty span and the following code:

$('input').on('input', function(){
    $('span').text(this.value);
});

When I type in the input the same text appears in the span. But when I go to another page in the same browser tab and then go back again, the input field is still filled in, but now the span is empty.

How can I make it so that when you go back to the page the span keeps the value of the input?

Thanks!

Edit: I think I've found the solution.

$('input').on('input', function(){
    $('span').text(this.value);
}).not(function(){
    return this.value == '';
}).trigger('input');
Answer:1

The behavior you are experiencing is normal if you are returning to the page by hitting the "back" button or browsing from your local hard drive - - you are seeing the cached version of the page in that case. Form fields can cache their values, but a span doesn't have a "value" per se, it has content and that content isn't cached.

If you were to return to the page by clicking a link or typing an address (things that cause an HTTP request), the text field will not show the last value from the previous time, it will show its default value.

In addition to the code you already have, you just need a script that fires off as soon as the page is ready that resets the span to the value of the input.

// You must already have a document.ready event handler, so just add to that:
$(function(){

  // This will restore the span's content to the value of the textbox
  $('span').text($('input').val());

  // This is your original code that wires up the textbox to a click
  // event handler
  $('input').on('input', function(){
    $('span').text(this.value);
  });

});
Answer:2

I have a dynamic object like this: { AB = "18722"; CF = "345555.4"; DT = "87789.33"; } And I need to sort it by it's value in descending format. I have searched for this but most answers I ...

I have a dynamic object like this: { AB = "18722"; CF = "345555.4"; DT = "87789.33"; } And I need to sort it by it's value in descending format. I have searched for this but most answers I ...

I have built a dropdown list, but I am not entirely happy with it. I am struggling with how to change the value of the default state, with the value of the clicked list item. The HTML: <div class=...

I have built a dropdown list, but I am not entirely happy with it. I am struggling with how to change the value of the default state, with the value of the clicked list item. The HTML: <div class=...

  1. change value in dictionary python
  2. change value in dataframe
  3. change value in pandas dataframe
  4. change value in sql
  5. change value in list python
  6. change value in hashmap java
  7. change value in array javascript
  8. change value in r
  9. change value in dictionary c#
  10. change value in dataframe r
  11. change value in numpy array
  12. change value in table sql
  13. change value in arraylist java
  14. change value in dataframe python
  15. change value in array python
  16. change value in column pandas
  17. change value in object javascript
  18. change value in tuple python
  19. change value in array php
  20. change value in map java

Meteor accounts uses the following function to validate password format: validatePassword = function (password) { if (password.length >= 6) { return true; } else { loginButtonsSession....

Meteor accounts uses the following function to validate password format: validatePassword = function (password) { if (password.length >= 6) { return true; } else { loginButtonsSession....

  1. custom password validation in angular 7
  2. custom password validation in angular 4
  3. custom password validation in jquery
  4. custom password validation in angular
  5. custom password validation in angular 6
  6. custom password validation in angularjs
  7. custom password validation in angular 5
  8. custom password validation in angular 8
  9. password custom validation in asp.net

A function whose declared type is neither 'void' nor 'any' must return a value. I have a createOrder function, that is supposed to return a promise. I have database calls in the function, and I'd ...

A function whose declared type is neither 'void' nor 'any' must return a value. I have a createOrder function, that is supposed to return a promise. I have database calls in the function, and I'd ...