How do I use “document.getElementById().innerHTML” with a onClick button to import files
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey there! 👋
I’ve not used Squarespace in a while but as far as I can tell bes on their docs here this shold be doable:
https://support.squarespace.com/hc/en-us/articles/205815908-Using-code-injection#
You should be able to:
Add a Code Block:
Insert Your HTML and JavaScript:
Here’s a simple example where clicking a button loads the content of a file into a div on your page:
<!-- Your HTML -->
<button onclick="loadFile()">Click to Load Content</button>
<div id="content"></div>
<!-- Your JavaScript -->
<script>
function loadFile() {
fetch('https://your-site.squarespace.com/s/yourfile.txt')
.then(response => response.text())
.then(data => {
document.getElementById('content').innerHTML = data;
})
.catch(error => console.error('Error loading file:', error));
}
</script>
loadFile()
function.fetch()
function grabs the content from a file (like a .txt
file) and then loads that content into the div with the ID content
.You’ll need to make sure the file you want to load is hosted somewhere accessible:
Upload the File:
Update the JavaScript:
'https://your-site.squarespace.com/s/yourfile.txt'
in the fetch()
function with the URL of your uploaded file.After you’ve added the code, save your changes and preview the page. When you click the button, it should load the content from the file right into your page.
Let me know how it goes.
- Bobby
You can add JavaScript to your Squarespace site by using Code Injection. Here’s how:
<script>
tags in the Footer section (recommended for site speed).<script>
function myFunction() {
alert("Hello World!");
}
</script>
<script>
tagsGet paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.