Solved

Jquery filter list

Posted on 2013-10-22
4
384 Views
Last Modified: 2013-11-30
Hey Experts,

I need to develop a filter using jquery and ajax. The list of items toggles information based upon click.


<a href="">one</a>
<a href="">two</a>
<a href="">three</a>
<a href="">four</a>


<div class="item" data-info="one">1</div>
<div class="item" data-info="two">2</div>
<div class="item" data-info="three">3</div>
<div class="item" data-info="four">4</div>

Open in new window

0
Comment
Question by:yando18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 39593179
You mean something like this?
<!DOCTYPE html>
<head>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
	<style>
		.item {display:none;}
	</style>
</head>
<body>
	<a href="#" onclick="$('#i1').toggle();">one</a>
	<a href="#" onclick="$('#i2').toggle();">two</a>
	<a href="#" onclick="$('#i3').toggle();">three</a>
	<a href="#" onclick="$('#i4').toggle();">four</a>


	<div id="i1" class="item" data-info="one">1</div>
	<div id="i2" class="item" data-info="two">2</div>
	<div id="i3" class="item" data-info="three">3</div>
	<div id="i4" class="item" data-info="four">4</div>
</body>
</html>

Open in new window

0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39593281
I think in the previous solution you want to do a return false on the onclick ...

Here is a solution that does not rely on inline onclick event handler
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
    $('a').click(function(e){
        // Prevent default behaviour on the <a> click
        e.preventDefault();
    
        // we use the href to find the id of the div to open
        // in a non JS environment this breaks down to an in page link
        var id = $(this).attr('href');
        
        // only process this if it is a click on a non visible anchor
        if (('#'+$('.item:visible').attr('id')) != id) {
            // Hide all the div's
            $('div.item').hide(200, function() {
                // when the divs are hidden - show the one linked to the <a> clicked
                $(id).show(500);
            });
        }
    });
});
</script>
<style type="text/css">
.item {
    border: 1px solid blue;
    padding: 20px;
    display: none;
    margin: 10px 0;
}
.item:nth-child(2n) {
    border: 1px solid red;
}
</style>
</head>
<body>
<a href="#one">one</a>
<a href="#two">two</a>
<a href="#three">three</a>
<a href="#four">four</a>
<div class="item" id="one" data-info="one">1</div>
<div class="item" id="two" data-info="two">2</div>
<div class="item" id="three" data-info="three">3</div>
<div class="item" id="four" data-info="four">4</div>

</body>
</html>

Open in new window

0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question