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 6685373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:03:01+00:00 2026-05-26T05:03:01+00:00

How can I write this jQuery more efficiently? The goal is to be able

  • 0

How can I write this jQuery more efficiently? The goal is to be able to click on a menu item in one div (left column), and change the contents in another div (right column). Here’s what I have so far:

$(document).ready(function(){
$('#content1').hide();  
$('#content2').hide();
$('#content3').hide();
$('#content4').hide();      

$('#link1').click(function(){ 
$('#content1').show('slow');  
$('#content2').hide();
$('#content3').hide();
$('#content4').hide();  
});

$('#link2').click(function(){ 
$('#content1').hide();  
$('#content2').show('slow');
$('#content3').hide();
$('#content4').hide();  
});  

$('#link3').click(function(){ 
$('#content1').hide();  
$('#content2').hide();
$('#content3').show('slow');
$('#content4').hide();  
});  

$('#link4').click(function(){ 
$('#content1').hide();  
$('#content2').hide();
$('#content3').hide();
$('#content4').show('slow');  
}); 
});

UPDATE:
This is the latest jQuery code I have that I can’t get to work. When I navigate to my page, all the content currently appears, but should be hidden. When I click on the links, nothing happens. All sections remain showing

$(document).ready(function() {
    var contents = $('.content')
    $('#nav a').click(function(e){
        e.preventDefault()
        var target = $(this.href)
        target.show()
        contents.not(target).hide()
    });
});
  • 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-26T05:03:02+00:00Added an answer on May 26, 2026 at 5:03 am

    Here we go:

    $(document).ready(function(){
        var contents = $('#content1, #content2, #content3, #content4');
    
        $('#link1, #link2, #link3, #link4').click(function(){
            // get the number at the end of the ID
            var n = this.id.match(/\d+$/)[0]
            contents.not('#content'+n).hide()
            $('#content'+n).show()
        })
    })
    

    But, this is error prone and not very mantainable, if not convoluted. This is why we have classes. Give a common class to your “content” elements, and you probably already have a meaningful structure for the links (which in turn should point to their respective target’s IDs):

    <ul id="nav">
        <li><a href="#content1">Content 1</a></li>
        <li><a href="#content2">Content 1</a></li>
    </ul>
    
    <div id="content1" class="content">...</div>
    <div id="content2" class="content">...</div>
    

    So you can turn this into something more straight-forward:

    var contents = $('.content')
    
    $('#nav a').click(function(e){
        e.preventDefault()
        var target = $(this.href)
        target.show()
        contents.not(target).hide()
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
If I'm using a Hashtable , I can write code like this: object item
How can I write a query similar to this one in LINQ to SQL
I'm starting to write more jQuery plugins within their own namespace so I can
Via Firefox+Firebug one can write out to the console a jQuery object and the
This is my first attempt to write a jQuery plugin, this one is supposed
How can I write this in such a way that $counter value is 1
So I know I can write my own HTML-encoding function like this: function getHTMLEncode(t)
Eg. can I write something like this code: public void InactiveCustomers(IEnumerable<Guid> customerIDs) { //...
In Oracle, you can write: update t set xml = updateXML(xml, '/a/b/text()', 'gaga') This

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.