jQuery CSS Class playground

Today we will play with jQuery CSS class methods such as addClassremoveClasshasClass, and toggleClass and learn some basic implementation to modify the HTML styles on the fly.

jQuery CSS Class Methods:

Let’s understand this is a small piece of code. First things first, You must include the jQuery library in the <head>…</head>  tag.

<script src=PATH_TO_JQUERY/jquery.min.js></script>

Now our page has the jQuery library so let’s add some HTML and CSS to play with the jQuery CSS Class methods.

HTML Code:

<div id="elem">
  This is a content panel
</div>

<p>
  <a href="#" >addClass</a> /
  <a href="#" >removeClass</a> /
  <a href="#" >toggleClass</a> /
  <a href="#" >hasClass</a>
</p>

Here we have a DIV tag with ID set to element and we will use this ID to play with jQuery CSS class methods. Below that, we have a paragraph element and four links with different class names. Let’s write some CSS code and I will explain the functionality of each link when we add jQuery code.

 CSS Code

#elem{
	width: 400px;
	text-align: center;
	padding: 20px 0;
	background: #ffffff;
	border: 1px solid #ddd;
	margin: 100px auto 30px auto;
}

.hover{
	background: #cc0000 !important;
	color: #fff !important;
	border: 1px solid #990000 !important;
}

In the CSS styles, I’ve added some styles to our DIV#elem element and also created a class named hover which will change the background, color, and border of the DIV#elem.

Now let’s add some jQuery code and learn how these jQuery CSS methods work.

$(document).ready(function(){
	// Add the class
	$('a.add-class').click(function(){
		$("#elem").addClass('hover');
		$("#elem").html('Class added ".hover"');
		return false;
	});

	// Remove the class
	$('a.remove-class').click(function(){
		$("#elem").removeClass('hover');
		$("#elem").html('Class removed ".hover"');
		return false;
	});

	// Toggle the class and check if the class has been already added or not
	$('a.toggle-class').click(function(){

		$("#elem").toggleClass('hover');

		if($("#elem").hasClass('hover')){
			$("#elem").html('Class added ".hover"');
		}else{
			$("#elem").html('Class removed ".hover"');
		}

		return false;
	});

	// Checks if the class has been already added or not
	$('a.has-class').click(function(){

		if($("#elem").hasClass('hover')){
			$("#elem").html('Class found ".hover"');
		}else{
			$("#elem").html('Class not found ".hover"');
		}

		return false;
	});

});

In the JQuery code, I’ve used the click trigger for all four links we added in the HTML code above. Also, I’ve used return false;  so when we click on the link it should not redirect the user to any page or use # in the current page URL. Also, I’ve added another line of code to modify the HTML of the #elem element when we click on these links. Don’t get confused with the .html() method, this is not required, I just added it to explain the functionality.

The first link with class .add-class will add the .hover class to the #elem element. If you want to add multiple classes with one trigger you can specify different class names separated with a space as we normally do in normal HTML code. Once we click on this link, .hover class will be added to the #elem HTML element and the inner HTML will be changed and show us the result. You can click on the address link above and see it in action.

The second link with class .remove-class will remove the .hover class from the #elem element. If the element doesn’t have a .hover class, it won’t do anything so the remove class will be used only to remove any applied class.

The third link with class .toggleClass will toggle the .hove class on #elem element. If the element does not have a .hover class it will add the same otherwise vice versa. In this code, I’ve used the jQuery CSS method hasClass to check if the element has the .hover class or not. and the content of the #elem element is changed accordingly.

The fourth link will only check if the class .hover is applied to the #elem element or not and change the HTML code of the #elem element accordingly.

I hope this will clarify any doubts on using these methods, in case you have any questions or suggestions, please use the comments below to start a conversation on this topic.

contact us

Web development Services

Does Website Redesign Services Make Any Difference in Business?

Are you wondering whether you should redesign the website? This is the right blog to go through. We recently had a massive Website Redesign Services on various platforms like Shopify, Magento, Laravel, that showed how much difference a website redesign can make. Unlike rebranding, which focuses on refreshing the image of your brand, redesigning the site is a completely new design that you are developing for the site to help you achieve your unique business goals. The goal of website design is to create a page that looks good, but you also want to design the page to deliver leads and sales to your business.

If you are dealing with a bad user experience, do not get the results you want, or need to change your brand marketing strategy, a redesign of the website is necessary. Think about redesign if you are not getting enough traffic or conversions.

Website Redesign Services is a complex undertaking that should only be considered if your current website has significant deficiencies. One of the first steps in deciding whether to redesign or update your website is to look at the design and content of your website. You can also go through the different types of website design, content, and user experience design to get a better understanding.

If you have any questions about web design, the team at IOGOOS Solution will assist you.

contact us

If you have an eCommerce store and you just need to clean up the content with a clear call-to-action and customize navigation to improve the user experience, you have website redesign services or Shopify Development Services. To ensure that you are fully aware of the steps you need to take to redesign your website, you need to think seriously about why you need or want to redesign your website. When you begin to evaluate the need for a website redesign or update, follow these five basic steps. Play with the design and content of your existing website and experiment with new features and features.

A redesign of the website can help to organize the information on your website more logically and improve the overall user experience for your visitors. Redesigning a website has features like becomes mobile-friendly, meet business goals easily. This includes responsive design, and if that is the case, redesigning the web pages can help ensure that everything flows seamlessly into one another and provides a consistent experience for your website users.

Introducing an evolutionary approach to redesigning websites and dramatically improving your website every day will protect against the risks of redesigning websites. With the redesign of the evolutionary website, you are often testing big and important changes and always aware of what is going on.

If you find something that needs updating, it may be time for your company to develop a new brand or web design. With a new brand, you might also feel that it’s time to launch a true-to-scale website that will be redesigned with a new logo, LOGO Design, and branding.

Investing in a redesign is an important option to consider, and you can work with a web design agency or decide how much you want to redesign. Overall, it depends on your ability to absorb the changes your business can choose to make to a website redesign or website redesign, but investing in the redesign itself is another important option to consider.

If your brand has changed significantly since your website was designed, it might be time to start redesigning the site. If your site is not able to meet new needs, such as additional pages, you may need to start planning for a website redesign. Content strategies are constantly changing, and in some cases, a website redesign is a perfect time to change the website platforms to your liking.

If your website lacks brand elements, it may be useful to redesign it when it has been updated, but rebranding as a business is not the same as redesigning a website. Decisions taken in this way to redesign the website will undoubtedly have an impact on the design and messages reflected on the website, and you must redesign your websites to reflect the significant changes in branding and ensure a consistent appearance. In some cases, websites can be redesigned to adapt to your brand’s improved communication, such as the new logo and logo design, and new content.

Get connect with IOGOOS for complete Website Solutions, WordPress Development Services, SEO, Online Promotion, Local SEO.

Also Read:
How to Increase organic traffic for better ranking AND Lead Generation Tactics

Enquiry Now

When We Work Together

We can create something incredible

arrow
HQ INDIA
HQ INDIA
C-31, Milap Nagar,
Uttam Nagar, New Delhi,
Delhi 110059
USA
USA
6715 Backlick Rd Suite 202
Springfield,
VA 22150, USA
AUSTRALIA
AUSTRALIA
2/51, Lane Cres,
Reservoir, VIC
3037, Australia
CANADA
CANADA
61 Payzant Bog Road, Falmouth, NS, B0P 1P0, CANADA
UK
UK
3rd Floor, 131 City Road, London, EC1V 2NX, United Kingdom
UAE
UAE
Boutik Mall, Al Reem Island - Abu Dhabi, UAE
X

Let Us Call You Back

  • India+91
  • United States+1
  • United Arab Emirates+971

Your phone number is kept confidential
and not shared with others.