Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:08:55+00:00 2026-05-31T18:08:55+00:00

I made a simple jquery script to sort content of the page on clicking

  • 0

I made a simple jquery script to sort content of the page on clicking of classes… in this example, all products, windows, or macintosh. The script works just as I want it to EXCEPT since I am using a # in the href the window scrolls… is there anyway to stop the window from scrolling and staying exactly where it is when the user clicks on one of the links?

Also, I put the script together pretty quick – if anyone wants to offer some optimization please go ahead…

basic html :

<a class="all" href="#">All Products</a>
<a class="win" href="#">Windows</a>
<a class="mac" href="#">Macintosh</a>

<div class="windows">1 win</div>
<div class="macintosh">2 mac</div>
<div class="windows">3 win</div>
<div class="windows">4 win</div>
<div class="windows">5 win</div>
<div class="macintosh">6 mac</div>
<div class="windows">7 win</div>

the script :

var $zproducthide = jQuery.noConflict();
$zproducthide(document).ready(function() {

$current = 'all';

$zproducthide(".all").click(function () {
if ($current != 'all'){
       $zproducthide(".windows").hide();
       $zproducthide(".macintosh").hide();
       $zproducthide(".windows").fadeIn(1500);
       $zproducthide(".macintosh").fadeIn(1500);
       $current = 'all';
}
});

$zproducthide(".win").click(function () {
if ($current != 'windows'){
       $zproducthide(".windows").hide();
       $zproducthide(".macintosh").hide();
       $zproducthide(".windows").fadeIn(1500);
       $current = 'windows';
}
});

$zproducthide(".mac").click(function () {
if ($current != 'macintosh'){
       $zproducthide(".windows").hide();
       $zproducthide(".macintosh").hide();
       $zproducthide(".macintosh").fadeIn(1500);
       $current = 'macintosh';
}
});
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:08:57+00:00Added an answer on May 31, 2026 at 6:08 pm

    How are you?

    New answer (some optimization included):

    //Start with the default class
    $current = 'all';
    
    //Setup an object which is configured as { class : 'jQuery selectors' }
    //This is neat because you can add more items and only modify this array like this:
    //arr = {'lin': '.linux', 'win' : '.windows', 'mac' : '.macintosh' };
    arr = {'win' : '.windows', 'mac' : '.macintosh' };
    
    //Get all the object properties in order to make it dynamic (so you don't have to add new classes manually, only in our "arr" object
    var sel = '';
    $.each(arr,function(k,v){ sel += ',.' + k; });
    //at this point aSelector equals to ",.win,.mac";
    
    //create the .all selector
    arr.all = sel;
    
    //Equals to $('.all,.win,.mac'
    $('.all' + sel,function(){
    
      $current = $(this).attr('class');
    
      $(arr['all']).hide(); //Hide all elements
      $(arr[$current]).fadeIn(1500); //Show only the current one (in case it's 'all', all elements will be shown)
    
      //Prevent default behaviour!
      return false;
    });
    

    Original answer:

    The way to prevent this is to return false in the click event, this way, the default behaviour won’t execute! So:

    //Select all the anchors with the desired classes
    $("a.all,a.win,a.mac").click(function(){ return false; });
    

    I believe this should work!

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple jQuery script, which gives me error that delay is
I have made a simple chat script using jquery and AJAX but the huge
Made a simple script that hide/show a hidden div. The page automatically adjusts its
I made a simple script in jQuery that takes an image and slowly rotates
I made a simple script that tells you what's selected using jQuery remove class
I have started using jQuery and rails. I have made a simple form that
On Windows, testing different OSes is made simple using VMs. Is there a simple
I made this simple function to filter the data. I add the symbols that
I need your help. I have made a really clean and simple example to
I'm expanding on what used to be a pretty simple jQuery accordion script, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.