Using Wordpress and JavaScript to dynamically hide a button if a product in the online store does not have an SKU

I'm encountering an issue on my Wordpress site where products with variations are not displaying the inner text on a certain element, despite the fact that the text is present when I inspect the element. Here's the code:

const makerBtn = document.getElementById("maker-button");
const sku = document.getElementsByClassName("sku");
    
const newLink = document.createElement("a");
newLink.innerText = "Buy at Maker's Site";
newLink.className += "fusion-button button-flat fusion-button-default-size button-custom button-3 fusion-button-default-span fusion-button-default-type";
newLink.addEventListener("click", ()=> {
        newLink.target = "_blank";
        newLink.href = sku[0].innerHTML.toString();
});
        
makerBtn.appendChild(newLink);

I've added URLs to some products in the sku and if they exist, clicking the button will take you to a new site. However, I'm trying to find a way to hide the button if there is no sku. If I use this code:

if (sku[0].innerHTML === "") {
         newLink.style.display = "none"; 
}  

It works, but it also hides the button for products with variations because their sku[0].innerHTML appears as an empty string even though the console log shows the URL is present.

I'm struggling to identify which property of these variable products can be used in a conditional statement to differentiate their behavior. Any assistance would be greatly appreciated. Thank you!

Answer №1

Why not give this a shot:

if (sku[0].innerHTML === "") {
    newLink.style.opacity= "0.0"; 
}  

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging the power of Ajax and Javascript to handle and process conditional if/else statements

Hey there, I'm new around here so I hope I got this right. Currently, I'm diving into the Ajax for Dummies book after enjoying the PHP one years ago. However, I've hit a roadblock with my first real Ajax program. It took me ages to locate th ...

Different ways to manipulate the CSS display property with JavaScript

Having trouble changing the CSS display property to "none" using JavaScript. Check out my code: <div class="mydiv" onClick="clickDiv1()">hello</div> <div class="mydiv" onClick="clickDiv2()">hi</div> <div class="mydiv" onClick=" ...

Find unique numbers within a specified range using NodeJS

I need to update my arts on an NFT test via OpenSea API, but I'm facing an issue where the numbers are being repeated. Is there a way to select a number within a range that does not repeat? Here is my current code: const opensea = require("opense ...

When making a request for "/example.php/" the CSS does not seem to take effect, however, the content is displayed properly

Currently, I am working on a PHP script where I have divided each section into separate .php files and included them using the include() function. Here is an example of how everything is set up in different folders: header.php footer.php etc.. When acc ...

load two separate instances of wp-load

I am facing a challenge where I have 2 separate WordPress installations on the same server with different databases. My goal is to showcase the latest posts and image galleries from both of my blogs on an external front page. Unfortunately, I cannot utiliz ...

Obtain text content using JQuery and AJAX rather than retrieving the value

I am struggling with a dynamic table that needs to perform calculations before submitting, requiring the presence of values. However, I want to submit the text from the options instead of the values. Despite trying various approaches, none of them seem to ...

originalRenderPage has not been declared

I encountered an issue while working on my new nextjs app. The problem arose when I tried to add a carousel using props in my project, resulting in an error stating that 'originalRenderPage' in Document.js is not defined. Any assistance would be ...

Display specific posts on WordPress using their unique IDs

I'm facing a bit of a dilemma here - I really need to retrieve one or more posts solely based on their unique ID. Right now, my current code looks like this: <?php $post_id = 1; $queried_post = get_post($post_id); $content = $queried_post->post ...

How to set a maximum width in IE8 when using width:100%?

After researching online, I am still unable to get it to work. I have a basic HTML layout that I have been testing across different browsers. Knowing the quirks of IE, I wasn't surprised when the max-width CSS code didn't function as expected. Ho ...

"Implementing CSS inline styles for improved appearance on a Safari browser when visiting

Having some trouble using CSS to create rounded corners on my Facebook fan page. Everything works fine except for Safari browser. I've tried inline styles and a separate stylesheet, but no luck. Any help would be appreciated. My CSS for rounded corne ...

How to style material-ui input with outlined variant using css

Currently, I am attempting to replicate the material-ui outlined input. The background color and input styles are different, so simply setting the label position to absolute and pushing it up is not working for me. Any suggestions on how to achieve this? ...

Issue with AJAX/Javascript functionality

Hey there! I'm currently working on an ajax call to a function called build_Array. This function is supposed to break down a string named myString, which contains "Call 1-877-968-7762 to initiate your leave.,1,0,through;You are eligible to receive 50% ...

Troubleshooting the Issue of PHP Variables Not Being Assigned to Javascript Variables

I am currently struggling with an issue. I am trying to assign a PHP value to a variable in Javascript. Here is what I have attempted: <script> JSvariable = <?php echo $PHPvariable; ?>; </script> However, this approach is not yieldi ...

A significant number of middleware packages, such as compress, are no longer provided as part of the

I recently added [email protected], [email protected], [email protected] and [email protected] (just to be safe). However, I am encountering this error message when trying to execute sails lift /Users/myuser/myproject/backend/node_modu ...

What is the best method for installing Raphael.js using bower?

Currently, I am attempting to incorporate Raphael.js into my project in a highly modular manner. Since Raphael has a registered bower component, utilizing that option seemed like the most logical choice. Following some instructions from the Snap.svg readm ...

Dealing with throwing Exceptions in jest: A guide for developers

I have developed a method that throws an exception when the provided password does not match a regex pattern. I attempted to handle this in Jest. it('Should prevent insertion of a new user if the password doesn't match the regex', async () ...

What is the process of transferring JavaScript code to an HTML file using webpack?

I am looking to display an HTML file with embedded CSS, fonts, and JS (not linked but the content is inside). I have the CSS and fonts sorted out, but I am struggling to find a solution for the JavaScript. My project is based on Node.js. ...

Ensure menu options are aligned to the right using material-ui

My material-ui menu currently has the following setup: <span> <Link to="/issues"> <Button style={isActive(history, "/issues")}>Issues </Button> </Link> <Link to="/users"> <Button style={isActive(his ...

How can I retrieve an updated object array within an extended class in JavaScript?

Hey everyone, I am new to working with ES6 classes. Currently, I am trying to inherit an initialized object (this._obj) with updated data in the class B, but I am encountering an issue where I am getting the length of the initialized object instead of the ...

how to put an end to sequential animations in React Native

Is there a way to pause a sequenced animation triggered by button A using button B? Thank you for your help! ...