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!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
1. Inject JavaScript into Squarespace
You can add JavaScript to your Squarespace site by using Code Injection. Here’s how:
Method 1: Inject JavaScript Site-wide
<script>
tags in the Footer section (recommended for site speed).Method 2: Add JavaScript to a Specific Page
<script>
tagsHey 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:
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:
loadFile()
function.fetch()
function grabs the content from a file (like a.txt
file) and then loads that content into the div with the IDcontent
.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 thefetch()
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