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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:14:29+00:00 2026-05-26T21:14:29+00:00

The question I would like to ask is if the Javascript snippet below is

  • 0

The question I would like to ask is if the Javascript snippet below is well formatted and structered in terms of Javascript/jQuery syntax, readability and speed.

The function of this piece of code is to provide a tabbed interface where through clicking the tabs different parts of content are shown. I could of course use a plugin, however I’d like to keep it simple. It works fine the way I wrote it now, but I would like to know if there are improvements to be made in order to learn a correct style and manner of coding.

// Wrap in anonymous function to not pollute the global namespace
(function() {
    // Hide all wrappers. 
    // If javascript is disabled then all wrappers have to be visible.
    $('#main .wrapper').hide();

    // Show selected wrapper. This class is set in the HTML that is loaded.
    // The wrapper has the id of the html, perhaps using href is better?
    var html = $('.nav a.selected').html().toLowerCase();
    $('#main #' + html).show();

    // Show selected wrapper on click event
    $('.nav a').click(function(){
        // Remove selected class all tabs
        $('.nav a').removeClass('selected');

        // Hide all wrappers and remove selected class from all wrappers
        $('#main .wrapper').hide();

        // Add selected class to new tab
        $(this).addClass('selected');

        //Show selected wrapper
        var html = $(this).html().toLowerCase();
        $('#main #' + html).show();

        return false;
    });
}());

I hope you can give me some advise and suggestions for correct and elegant coding. Thanks in advance!

  • 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-26T21:14:29+00:00Added an answer on May 26, 2026 at 9:14 pm

    None of the selectors are optimized for speed or cached. As jQuery goes, I’ve seen worse and it will probably work fine. As high-performance, professional JavaScript goes, it gets a D-.

    //side comments are not really there in prod code, this is just for example purposes
    //this code assumes the DOM nodes in main wrapper have no ids and can not get them, obviously ids make a huge difference
    $(function () { //make sure the document is ready, not needed if you place in <script> at the bottom
        //single var statement validates JSLint
        "use strict";
        var doc = document,
            main = doc.getElementById("main"), //faster than sizzle
            $main = $(main), //faster instantiation of jQuery
            $wrapper = $main.children(".wrapper"), //faster than find, use if possible
            $nav = $(".nav"), //cached
            html;
        //initialize
        $wrapper.hide();
        html = $nav.children(".selected").eq(0).html().toLowerCase(); //stop on first
        $(doc.getElementById(html)).show();
        //add handler
        $nav.delegate("a", "click", function () { //delegate outperforms multiple handlers
            var $this = $(this); //cached
            if ($this.is(".selected")) {
                return false; //return early if this is already selected
            }
            $this.siblings(".selected").eq(0).removeClass("selected");  //you shouldn't need to find more than one
            $this.addClass("selected");
            $wrapper.hide();
            //TODO add ids
            $(doc.getElementById($this.html().toLowerCase())).show();
            return false;
        });
    });
    

    If you want to work on your performance, get very familiar with the work of Nick Zackas and other performance gurus who do presentations and write books on the subject. Rely as little as possible on jQuery. Use JSPerf religiously. Question everything you write and leave comments when you’re unsure or think it can be done better in future implementations.

    Good luck to you, the results are worth the journey.

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

Sidebar

Related Questions

I would like to ask a rather, general, high-level introductory kind of question regarding
Just like Carl's question over here I would like to ask you (because I
I know this is a weird question to ask, but I would like to
This question would probably apply equally as well to other languages with C-like multi-line
I would like to ask a question about a new and large scale web
I would like to ask some logic question here. Let say I have a
I would like to ask a theoretical question. If I have, for example, the
I would like to ask a question. Any help will be highly appreciated. I
I would like to ask a question regarding a strange behavior I face using
I would like to ask you a question regarding which concurrency facility (CMutex, CSemaphore,

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.