If an element contains a specific class, then set its display property to none

I am attempting to utilize the hasClass method in JavaScript to determine whether a div should be hidden (display: none) or kept visible as usual.

Below is the snippet of code I currently have:

   if (!$('cool').hasClass('hot')) {
   }

If the class "hot" is present, I would like the div to be hidden. What code should I use to achieve this?

UPDATE: After reviewing the comments, I am still unsure about how hasClass functions, but it appears to check for HTML tags.

All I need is for it to verify whether a div has the "hot" class and then hide it if it does.

Answer №1

Instead of using an if statement, focus on selecting elements that meet your criteria from the start.

jQuery('cool.hot').addClass('foo'); // Assuming .foo has { display: none; }

Note: The HTML does not include any <cool> element.

Answer №2

Another alternative is to utilize the :not() selector according to the original premise provided by the OP -

(It is assumed that 'cool' and 'hot' are CSS classes in this particular example)

$('.cool:not(.hot)').css({'display':'none'});

An additional approach would be to employ the .not() method -

$('.cool').not('.hot').css({'display':'none'});

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

Setting an ID attribute with EditorFor

I have been working with a template for Views and utilizing a lot of JQuery to extract input from an EditorFor. Due to this, I prefer sticking with my current setup using EditorFor instead of something like <input type="text"> I recently added a dat ...

What methods does Angular2 use to differentiate between Light DOM and Shadow DOM within component views?

Currently, I'm engrossed in an intriguing article that delves into the concepts of host and visibility. In particular, it sheds light on the significance of the viewProviders metadata property within the Component decorator: The article discusses h ...

What is the Best Way to Create a Form Inside a Box?

Hello there! I am trying to create a form underneath the box labeled "New Referral," but I'm having trouble figuring it out. I would like to include sections for first name, last name, date of birth, phone number, email, and address under the box. Any ...

Comparing Yii's CHtml::link() function with the use of regular <a href=''></a> HTML tags

When using elements like yii CHtml::link, what are the recommended practices? I'm working on a button that needs to include an icon, text, respond to hover events, and be wide. Are there any benefits to using CHtml instead of a regular tag that can e ...

Navigating Divs Using jQuery

I am working with a class that has multiple divs, each with a unique id attached to it. I am using jQuery to dynamically cycle through these divs. This is a snippet of my HTML code: <div id ="result">RESULT GOES HERE</div> ...

Utilizing Bootstrap divs for multiline display in Yii2 platform

Currently, I am utilizing a list in Bootstrap CSS which is responsive and beneficial. However, I am faced with the challenge of creating a notification list with lengthy text akin to that on Facebook. My query pertains to the technique required to make th ...

Display an Array from a PHP File in HTML Using jQuery

Here is an example of my PHP script used to retrieve and display JSON data: $get_chat = "SELECT * FROM chat WHERE u_id1='$u_id' && u_id2=2 ORDER BY data DESC"; $run_chat = $conn->query($get_chat); if ($run_chat->num_rows > 0) { ...

Contrast between PHP and JavaScript output texts

Hey everyone, I'm dealing with a bit of an awkward situation here. I am trying to return a string variable from PHP to JavaScript and use it for a simple comparison in my code. However, the results are not turning out as expected. Initially, I send a ...

Unable to export to Excel using TableTools feature

Trying to export a DataTable view to Excel using the TableTools plugin, but encountering issues with the "Excel" button not working. Without a step-by-step tutorial for guidance, here's the code used in a test HTML: <!DOCTYPE html> <html ...

Navigating in Angular 2: Ways to route to a different page while keeping the HTML hidden from the app.component.html file on the new page

Just recently, I encountered a minor issue. I already have a good understanding of how to navigate between different pages, but there's one thing that bothers me - each time I switch to a new page, the app.component.html always appears at the top of t ...

Determine username availability using Jquery, Ajax, in combination with Flask and a Sqlalchemy database

Struggling with using Jquery and Ajax to verify the availability of a username. Despite checking the code multiple times, the loading image appears but the username messages do not show up, and even the loading image doesn't get hidden. Please assist ...

Making changes to JSON format post $.get function

Struggling to make sense of the JSON properties I receive from the server. Any advice on how to change the format would be greatly appreciated. Here is a snippet of the received data: [ { "date": "2006-07-01T00:00:00.000Z", "date_processed": "2 ...

Padding issues in navbar-expand for Bootstrap 4

I'm encountering a strange issue with padding in my navbar that seems to be dependent on the navbar-expand property. For instance, when I set it to MD, it appears like this: https://i.sstatic.net/czytc.png As you can see, the logo and menu button d ...

Using async/await with Axios to send data in Vue.js results in different data being sent compared to using Postman

I am encountering an issue while trying to create data using Vue.js. The backend seems unable to read the data properly and just sends "undefined" to the database. However, when I try to create data using Postman, the backend is able to read the data witho ...

When retrieving data from a PHP $_SESSION using an Ajax $_POST call, outdated information is being returned

I am encountering an issue that I cannot seem to figure out. Currently, my application consists of three pages: Home.php Nearmiss.php Reports.php Each of these pages includes code at the top with a specific "thispage" variable unique to that page. < ...

Enhance the responsiveness of Bootstrap 4 table columns to efficiently display content by widening the width

Incorporating Bootstrap's 4 responsive tables has proven beneficial for displaying tabular data. However, a challenge arises when some data cells are required to hold a substantial amount of information, such as a couple of lengthy sentences. In these ...

Voting Dynamics in Django: Ordering by Popular Vote

There is a lot of existing documentation on this topic, but so far, none of the solutions have worked for me. As the title suggests, my goal is to sort a set of objects using Django Voting based on the number of votes they have (from high to low). I have a ...

Enhance user experience with AngularJS Bootstrap autocomplete feature by automatically filling input box when hovering over dropdown options

I am currently implementing the AngularJs Bootstrap typeahead functionality. Typically, when we enter text that matches an option, the dropdown list appears. https://i.stack.imgur.com/395Ec.png What I aim for is to automatically fill the textbox with the ...

mongojs implementation that allows for asynchronous query execution without blocking

Forgive me for asking what may seem like a silly question, but I am struggling to make this work. Currently, as part of my learning journey with node.js and mongojs, I have encountered the following issue: Below is my server.js file server.get("/", funct ...

What is the best way to immediately update the state in a React functional component?

Having recently started learning React, I find myself struggling to understand the lifecycle of a functional component. Let's consider a scenario where I have multiple checkboxes labeled as: checkbox, a, b, c, and d, each corresponding to values a, b, ...