DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
jQuery

Switch List/Grid View using jquery

Share

In this tutorial you can get strong and good introduction to switch list grid view layouts of any HTML listing with jQuery.

A common web interface feature is dynamic view switcher and you’ll find sometimes buttons to change the layout of a web page from display lists to smaller grids.

Let’s coding the basic HTML:

The HTML isn’t very complicated but contains a lot of repetitive code. Towards the top I have included two links with the classes list and grid.

<div id="box">
<div class="buttons">
<button class="grid">Grid</button>
<button class="list">List</button>
</div>

<ul class="list">
<li>List 1</li>
<li>List 2</li>
<li>List 3</li>
<li>List 4</li>
<li>List 5</li>
<li>List 6</li>
<li>List 7</li>
</ul>
</div>

We will bind a click event handler onto these two buttons i.e. list and grid later on.

Read Also: Add table row dynamically in jQuery

Looking at CSS Styles:

Using jQuery we can change it between grid and list, then apply css for each.

#box ul { list-style: none; }
#box .buttons { margin-bottom: 20px; }
#box .list li { width: 100%; border-bottom: 1px dotted #CCC; margin-bottom: 10px; padding-bottom: 10px; }
#box .grid li { float: left; width: 20%; height: 50px; border-right: 1px dotted #CCC; border-bottom: 1px dotted #CCC; padding: 20px; }

jQuery Switch Effects:

Let’s look at the opening bind method:

$('button').on('click',function(e) {
if ($(this).hasClass('grid')) {
$('#box ul').removeClass('list').addClass('grid');
}
else if($(this).hasClass('list')) {
$('#box ul').removeClass('grid').addClass('list');
}
});

See DEMO Online.

So, this article should give you a strong introduction for manipulating your page layouts with jQuery and developing with the jQuery library can make your code JavaScript easier than ever before!

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Why is SEO a non-negotiable part of modern marketing?

In a world where digital presence is paramount, the question isn't whether you should do…

8 hours ago

Innovations in Hair Care: Exploring Breakthroughs for Lasting Results

Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…

1 day ago

How To Strengthen Your Brand-Building Efforts?

Your brand more than developing an attractive and creative logo and infectious motto. It's the…

2 days ago

How To Be Successful And Maximize Compensation In A Personal Injury Case

Introduction Are you someone who has suffered from a personal injury and want to file…

2 days ago

What Are The Biggest Challenges Of Working From Home?

Operating from home has emerged as one of the most popular ways of doing jobs…

3 days ago

Art As An Asset: Will NFT’s Help You Survive Hyperinflation?

If the consequences of our society’s ever-growing debt are what worries you, then it is…

5 days ago