I need a bit of help / guidance. I’m sending data from one page to another. The data is sent from one textbox to another (in different page).
The data gets sent correctly and received on the other page correctly; but the value does not get inserted inside the textbox when recived:
This is where i’m passing the data:
const form = document.getElementById('form');
const SyrdariyaTotal = document.getElementById('SyrdariyaTotal');
form.addEventListener('submit', function(e){
e.preventDefault();
const SyrdariyaTotalValue = SyrdariyaTotal.value;
localStorage.setItem('Syrdariya', SyrdariyaTotalValue);
console.log(localStorage)
window.location.href = "test.html"
I’m receiving it correct as the console.log shows the data has been received correct:
<input id="Syrdariya" type="text" onchange="adddata()">
const abc = localStorage.getItem('Syrdariya');
console.log(abc)
document.getElementById('Syrdariya').textContent = abc;
localStorage.removeItem( 'Syrdariya' ); // Clear the localStorage
localStorage.clear();
but on this line document.getElementById('Syrdariya').textContent = abc;
the data is not retrieved; not sure why the data is not getting inserted inside the text box.
The console log shows the data ha been passed.
Any guidance will appreciate it please.
Thanks
2
Answers
The text content of an element is the text descended from it. That is, its text node children, the text node children of its element node children, and so on.
Input elements are void elements. They cannot have any child nodes.
The text displayed in an input, and submitted with a form, is its
value
.The problem is the use of
textContent
for input fields. For<input>
elements, you need to set thevalue
attribute, nottextContent
.Change this line:
To this:
By adjusting that, the value should properly populate in the textbox on the receiving page.