Conceal a specific DIV class when another class is visible

In my WordPress project, I am working on a customization where the add to cart button on the product page should remain hidden until the customer answers all the questions. The plugin I am using offers options for the customers to select, such as Step 1, Step 2, Step 3, etc. Once they reach the last step, the "Continue" button is deactivated and its class changes to .owl-next button disabled.

I want to use this class as a trigger to display the add to cart button only when it becomes active, and hide it otherwise. Currently, I have been successful in hiding the add to cart button, but I am facing difficulties in making it appear when required. Below are the codes I have experimented with so far:

<script>
var aVisible = $('.owl-next.button').css('display');
if (aVisible == 'none') {
}
</script>

<script>
if($('.owl-next.button').length){
   $('.single_add_to_cart_button.button.alt').hide();
}
</script>

<div class="owl-nav"><a class="owl-prev button">Back</a><a class="owl-next button disabled">Continue</a></div>
<button type="submit" name="add-to-cart" value="267" class="single_add_to_cart_button button alt">Add to cart</button>

Changes made on the last step:

<div class="owl-nav"><a class="owl-prev button">Back</a><a class="owl-next button disabled">Continue</a></div>
<button type="submit" name="add-to-cart" value="267" class="single_add_to_cart_button button alt">Add to cart</button>

Answer №1

Make sure to add an event listener to the button and verify if it has the class disabled. If it does, display your button using the following code:

$(document).ready(function(){
   $(document).on('click', '.owl-next.button', function() {
     let aVisible = $(this).hasClass('disabled');
      if (aVisible) {
       $('.single_add_to_cart_button.button.alt').show();
       }
    });
});

The .hasClass() method checks for the presence of a specific class when the button is clicked and returns a boolean value. If true, it will show the button again.

UPDATE: To address the PHP syntax error:

If you're including the script in a PHP file, you may encounter an error. You can either replace <?php with <script> and ?> with </script> as mentioned here.

Alternatively, you can use:

jQuery(document).ready(function(){
   jQuery(document).on('click', '.owl-next.button', function() {
     let aVisible = jQuery(this).hasClass('disabled');
      if (aVisible) {
       jQuery('.single_add_to_cart_button.button.alt').show();
       }
    });
});

This approach prevents conflicts with PHP files.

If I wrap the script in <script> tags and place it in the header, I get "TypeError: $ is not a function" in the console

Always remember to include the JQuery library at the beginning of the script to avoid errors. If issues persist, wrap the code like this:

jQuery(document).ready(function($){
   $(document).on('click', '.owl-next.button', function() {
     let aVisible = $(this).hasClass('disabled');
      if (aVisible) {
       $('.single_add_to_cart_button.button.alt').show();
       }
    });
});

Following this method should resolve any problems, see jQuery Uncaught TypeError: Property '$' of object [object Window] is not a function for more information.

UPDATE 2: According to Wordpress stack exchange, a better way to handle the document.ready function is as follows:

(function($) {
    $('.owl-next.button').click(function() {
         let aVisible = $(this).hasClass('disabled');
          if (aVisible) {
           $('.single_add_to_cart_button.button.alt').show();
          }
    });
})(jQuery);

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

Using AngularJs to implement a $watch feature that enables two-way binding

I am in the process of designing a webpage that includes multiple range sliders that interact with each other based on their settings. Currently, I have a watch function set up that allows this interaction to occur one way. However, I am interested in havi ...

What are the steps to start up a NodeJS API using an Angular app.js file?

Currently, I am following various online tutorials to develop a web application using the MEAN stack and utilizing a RESTful API. I have encountered some challenges in integrating both the API server and Angular routes. In my project, I have a server.js f ...

Columns that adapt to different screen sizes while maintaining a fixed aspect ratio, limited to a maximum of two columns

I'm facing a challenge trying to blend a few elements together: My goal is to have two columns take up 100% of the browser width with a height that adjusts relative to this width. I want these columns to switch to one column when the viewport is sma ...

Can node.js serve as a more efficient and sophisticated replacement for greasemonkey with database capabilities?

Last week, I utilized Greasemonkey to automate tasks on a website by scraping data. However, this method presented challenges for two reasons: It relies on a GUI interface instead of a command line interface. I had to store all persistent information in ...

Unable to locate the input element using xpath

I am having trouble retrieving an input element using xpath as the return statement is false. Oddly enough when attempting to retrieve the label, which is the only sibling of the input, it returns true. I also tried to target it using input[@type="checkbo ...

The Element Ui component fails to update when the prop of the Vue component changes

In my project, I have a parent component and several child components which all make use of the same prop. This prop is an array of keys that are used for a dropdown menu in element.js. Initially, when the children render for the first time, they do not co ...

Could there be another script taking control of jQuery(window).resize()?

When testing the code in both Chrome and Firefox, I noticed that only the NATIVE RESIZE event is being logged: window.onresize = function(){ console.log( "NATIVE RESIZE" ) } jQuery(window).resize(function(){ console.log( "JQUERY RESIZE" ) }); Strangely, ...

Tips for switching a group of buttons within a userscript by clicking a single button?

Apologies if my wording is not clear, allow me to clarify. I am in the process of developing a userscript that will display a set of buttons below a main button when clicked. These additional buttons will serve different functions and should disappear whe ...

The z-index is not functioning properly in terms of layering correctly

Seeking advice on z-index... I'm experimenting with the code and trying to get my id="hidebar" to appear above the "content" and "content-inner" divs. Apologies if this is elementary, but I'm having some trouble =D CSS structure file: body { ...

The code snippet 'onload='setInterval("function()",1000)' is not functioning as expected

I need to continuously load the contents of an XML file into a specific HTML div every second. Here is the JavaScript code that I am using to parse the XML file: function fetchEntries() { if (window.XMLHttpRequest) req = new XMLHttpRequest(); ...

CSS/Jade/HTML: struggling to get boxes to align properly on the same line

I'm currently working with a Jade template that looks like this: extends layout block content h2 Characters and Portraits div(id="portraitsBox") - var portraits = ["Clown-Fox", "Dragon-Bear", "Fish-Bear", "Deer-Wolf", "Salamander-Ant", "Sid ...

Build an immersive experience by incorporating threejs into A-Frame to develop a spherical environment filled with 360-degree videos

I've been working on a VR project that involves 360° videos in VR. My concept was to construct a sphere and apply a 360° video as the material. I've already managed to create my own Sphere Component and map a 360° image onto it! Similar to t ...

Ajax pagination does not update the currently active link

Hello, I am attempting to implement ajax pagination using CodeIgniter. However, I have encountered an issue where the active link in the pagination does not change. Can someone please assist me with this problem? Here is my AJAX code: $(function() { a ...

Is it possible to customize the notification message displayed after clicking the save button in a listview within ng-admin?

While working on the listview/creation view, I am attempting to incorporate a custom notification message instead of the default one (please see the screenshot) that appears when the user clicks the save button. Is there a way for me to include a custom n ...

In Three.js, objects are consistently displayed in a hierarchical manner, with each one appearing on top

As a newcomer to JavaScript and Three.js, I managed to write some code that successfully renders a sphere representing Earth and a smaller sphere as a Satellite in orbit. However, a perplexing issue arises when the Satellite passes behind Earth – it rema ...

Can $.ajax be used as a replacement for $(document).ready(function()?

After conducting an extensive search, I am still unable to find a clear answer to my assumption. The code I used is as follows: <?php session_start(); if (isset($_SESSION['valid_user']) && $_SESSION['from']==1) { ?> ...

Flex Display with Bootstrap for Responsive Tables

Is there a way to display the scrolling of a Bootstrap table using a flex container? https://getbootstrap.com/docs/4.4/content/tables/#responsive-tables I am looking for the JSFiddle example with DIV elements. Removing the display:flex from .flex_containe ...

Storing array elements in a MySQL database through PHP and jQuery

Hello, I am in the process of populating a table by inserting multiple rows individually for each query. Here is the code snippet that shows how I am sending an array elements: The HTML markup: <form action="post.php" method="POST"> <h1>U ...

When using a browser as a client, encountering an HTTP 405 error when attempting to make a POST request via $.ajax

CONTEXT To begin, my Restify service on Node is running smoothly and functioning perfectly when accessed through a GUI HTTP client. This issue arises only when attempting to interact with the service via a browser. If a solution from the server-side is re ...

Setting a uniform width for all columns in a table using ReactJS

When working with variable amounts of data displayed as columns in a table, I struggled to maintain fixed widths for each column. Despite my efforts, the columns would stretch based on available space rather than obeying the specified widths. For example, ...