Copy content from an html tag in a file to a tag in another html file
all in peace? In Django, I need the content of an HTML tag to appear in another template. Using the code JS:
<script>
var source = document.getElementById("teste").innerHTML;
document.getElementById("texto").innerHTML = source;
</script>
<div id="teste">context</div>
<div id="texto"></div>
It even works in the same template, but if the destination tag is in another template, it doesn't work.
Any idea?
It won't work because the JavaScript can't persist the data across to another file.
When a user visits, /home
your HTML and JS is being sent to the client for that route only, so the route /about
can't see what content is on the route /home
. You've not said if this data is being retrieved from the server, so I'm going to assume it's not and let's look at a simple way to store data and access across routes using local storage (If a user accesses your site from another machine, they won't have access to the data. Also best not to store sensitive information and that it doesn't exceed 5 MB).
/home route
<h1>Home</h1>
<script>
localStorage.setItem("name", "Jhon")
</script>
/about route
<h1>About</h1>
<p id="name"></p>
<script>
const nameElement = document.getElementById("name");
nameElement.textContent = localStorage.getItem("name")
</script>
This is only if you want / don't need to keep the data on the server. If not you would need to send that when you render the template.