1

I have a popup modal in Shopify, I'm using text node instead of innerHtml for security concerns. However, everytime I open the popup modal, the text node keeps getting appended to my h1 tag. Is there any way to check if the node already has been appended? (I don't want to use a boolean value to check if text node has been appended)

html:

<h1 id="ProductHeading" class="product__title product__title--template"></h1> <h2 id="ProductHeadingModal" class="product__title product__title--template product__title--modal"></h2> 

javascript:

var title = document.createTextNode(product.title); // Product heading is an element with h1 tag var productHeading = document.getElementById("ProductHeading"); if(// how to check if element has no node?) { productHeading.appendChild(title); } 

the entire javascript block:

window.onload = () => { if (window.__shgProductInits.length) { window.__shgProductInits.forEach((ele) => { let proId = document.getElementById(ele.uuid); proId.setAttribute('url', ele.productHandle); proId.style.cursor='pointer'; proId.addEventListener('click', (e) => { let productHandle = e.target.parentElement.parentElement.parentElement.getAttribute('url'); fetch('/products/'+productHandle+'.js') .then((res) =>{return res.json()}) .then((product) => { console.log(product) var product = product; document.getElementsByClassName("product-modal")[0].style.display = "block"; var title = document.createTextNode(product.title); var productHeading = document.getElementById("ProductHeading"); var productHeadingModal = document.getElementById("ProductHeadingModal"); if(!(productHeading.hasChildNodes())) { productHeading.appendChild(title); productHeadingModal.appendChild(title); var price = document.createTextNode("$" + parseInt(product.price).toFixed(2)); document.getElementById("product-price").appendChild(price); } document.getElementById("product-image").src = product.images[0]; }); }); }); } 

ProductHeading itself is not a node (I think). And checking innerHtml for length doesn't work as it is always 0

Update:

I've added the conditional check, it still returns false everytime I open the modal.

My code:
enter image description here

My browser console:
enter image description here

My website displays:
enter image description here

Inspect element in browser:
enter image description here

2
  • Please post an html so along with the js so could provide a working example. becouse any of the answer below should have worked for you. Commented Dec 18, 2020 at 10:09
  • I've updated with the html and javascript. I'm using script tags on a liquid platform in Shopify Shogun (liquid is a templating language like html) Commented Dec 18, 2020 at 10:15

2 Answers 2

1

A couple of ways:

if (element.firstChild) { // It has at least one } 

or the hasChildNodes() function:

if (element.hasChildNodes()) { // It has at least one } 

or the length property of childNodes:

if (element.childNodes.length > 0) { // Or just `if (element.childNodes.length)` // It has at least one } 

So you can just write this

var title = document.createTextNode(product.title); // Product heading is an element with h1 tag var productHeading = document.getElementById("ProductHeading"); if(!(productHeading.hasChildNodes())) { productHeading.appendChild(title); } 

Referring this answer

Sign up to request clarification or add additional context in comments.

11 Comments

hi Alex thanks for the prompt answer, I've updated my issue - hasChildNodes doesn't seem to work
And if you add the braces inside your condition if(!(productHeading.hasChildNodes()) ?
yea I've updated to (!(productHeading.hasChildNodes())) still appears to keep appending
Okay I thought because in your screenshoots the braces were missing. Okay I will take a look at it
For me it works, did I miss something here? jsfiddle.net/2mxd4wjb/#&togetherjs=lC1IpBTMx0 I try to append it twice but it will only do it once because after the first insertion it has childnodes
|
1
if (productHeading.hasChildNodes()) { } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.