Jquery.load() function does not bring the textarea element up to date

Asked

Viewed 68 times

1

Well, I have a Chrome extension with page 1 and page 2, each with its script.js. On page 1 I can open a new tab/window when I click on a button, and when the new tab opens, the . page html 2 calls a search script, via Jquery.load(), load a div with an element <textarea> of page 1, except that: <textarea> comes with the initial value of the DOM when loaded/built, but before clicking on the X button of the page1, the <textarea> was edited - I entered a new value through a script, however, when the function .load query such page element 1 for the page2, it comes with the original value. The command in the.js script of page2 is as follows:

$("#divPagina2").load("../pagina1.html #idTextareaPagina1");

In short: in page . html 1, I define <textarea> worthwhile textareaOriginal, then, in the script.js of this page1, I update to (for example) textareaComValorAlterado, and just this changed value is not taken by . load(). Any hint?

  • load makes a GET request and returns the initial value, in order to get the new value, the initial must be exchanged for the new value or when changing, the new value be saved somewhere, such as Localstorage to be accessed by the second page

  • The .load retrieves what’s on the server, not what’s on the browser screen.

  • William Constamilam... and how do you do that, can you demonstrate? grateful.

1 answer

1


Use sessionStorage. It creates a temporary cookie that only holds for that page session (after you close the site, the cookie is deleted by the browser).

When clicking the button that opens the new tab, run this before the code that opens the new tab:

var texto = $("#idTextareaPagina1").val(); // pega o conteúdo do textarea
sessionStorage.setItem("textarea", texto); // cria o cookie

On the page of the new tab, you get the value of sessionStorage and plays in div:

var texto = sessionStorage.getItem("textarea"); // pega o valor do cookie
$("#divPagina2").text(texto); // insere na div o conteúdo do cookie

Note that if the textarea content has HTML codes, change the method .text() for .html() on the last line above.

  • Sam... thanks for the tip, although I tried with sessionStorage and it didn’t work out, since this is only to work on the same page/ tab, so I switched to the localStorage, and all right!

  • If and location are the same thing, the only difference is that one is temporary and the other is indefinite.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.