All things Shopify and commerce
I'm trying to display a text someone inputs into a field, and I found the code for it. The code itself works, but tying it into Liquid doesn't. Any idead? Here's the code:
{%- javascript -%}
const input = document.querySelector("input");
const log = document.getElementById("schrift").innerHTML;
input.addEventListener("input", updateValue);
function updateValue(e) {
log.textContent = e.target.value;
}
{%- endjavascript -%}
Solved! Go to the solution
This is an accepted solution.
document.addEventListener("DOMContentLoaded", function() { const input = document.getElementbyId("schriftzug"); const log = document.getElementById("schrift"); input.addEventListener("input", updateValue); function updateValue(e) { log.textContent = e.target.value; } });
This worked wonderfully, it was using the first input field it found, not the one I wanted!
Try This Code :-
document.addEventListener("DOMContentLoaded", function() {
const input = document.querySelector(".text-input");
const log = document.getElementById("schrift");
if (input && log) {
input.addEventListener("input", function(e) {
log.textContent = e.target.value;
});
}
});
<div class="input-display-container">
<input type="text" class="text-input" placeholder="Type something...">
<div id="schrift" class="display-output"></div>
</div>
If you require further help to optimize your store, please don’t hesitate to reach out.
This contribution will always benefit you and you will get my full help easily and you can contact me easily.
Contect On My Mail :-Mail@gmail.com
The issue is that you're saving the inner HTML (a string) instead of a reference to the element. In your code you have:
const log = document.getElementById("schrift").innerHTML;
Here, log becomes a string (the current inner HTML of the element with id "schrift"). Later, when you try to update it with:
log.textContent = e.target.value;
it fails because you can't set a property like textContent on a string.
Instead, you should store the element itself by removing the .innerHTML when you select it. Change the code to:
const log = document.getElementById("schrift");
Now, log is a reference to the DOM element and you can update its content like this:
log.textContent = e.target.value;
Liquid Tags: The {%- javascript -%} tag might be a custom tag provided by your theme or a specific setup. Make sure that this tag is correctly configured to output a <script> block on your page.
DOM Availability: Ensure that the elements (the input field and the element with id "schrift") are present in the DOM before this script runs. If they’re rendered later, consider wrapping your code inside a DOMContentLoaded event listener:
document.addEventListener("DOMContentLoaded", function() { const input = document.querySelector("input"); const log = document.getElementById("schrift"); input.addEventListener("input", updateValue); function updateValue(e) { log.textContent = e.target.value; } });
Element Selectors: Confirm that your selectors correctly match the elements in your HTML. For example, ensure there is an <input> element and an element with id="schrift" on your page.
Making these adjustments should resolve the issue and allow the input text to be displayed correctly.
I removed the innerHTML and added the DOM Availability, but I'm still stuck somehow. Is there an ideal place to put the code? The element exists, of course, I checked multiple times.
Can you share your store url with Screen shot
If you require further help to optimize your store, please don’t hesitate to reach out.
This contribution will always benefit you and you will get my full help easily and you can contact me easily.
Contect On My Mail :-Mail@gmail.com
My shop is still in development mode, but here's a screenshot of the html code
Liquid/JavaScript
{%- javascript -%}
const input = document.querySelector("input");
const log = document.getElementById("schrift"); // Fixed this line
input.addEventListener("input", updateValue);
function updateValue(e) {
log.textContent = e.target.value; // Changed from log to use the element directly
}
{%- endjavascript -%}
<!-- Add this HTML where you want it to appear -->
<input type="text">
<div id="schrift"></div>
This is an accepted solution.
document.addEventListener("DOMContentLoaded", function() { const input = document.getElementbyId("schriftzug"); const log = document.getElementById("schrift"); input.addEventListener("input", updateValue); function updateValue(e) { log.textContent = e.target.value; } });
This worked wonderfully, it was using the first input field it found, not the one I wanted!
Discover how to increase customer engagement on your store with articles from Shopify A...
By Jacqui Apr 23, 2025Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025