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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:07:34+00:00 2026-05-25T22:07:34+00:00

Is it possible to simplify the following jQuery using a for loop? $(#nav li

  • 0

Is it possible to simplify the following jQuery using a for loop?

$("#nav li a.h").mouseover(
  function () {
    if($(".content.h").is(":hidden")) {
      $(".content.h").fadeIn('fast');
    }
  }
);
$("#nav li a.e").mouseover(
  function () {
    if($(".content.e").is(":hidden")) {
      $(".content.e").fadeIn('fast');
    }
  }
);
$("#nav li a.o").mouseover(
  function () {
    if($(".content.o").is(":hidden")) {
      $(".content.o").fadeIn('fast');
    }
  }
);

I tried this, but no luck:

var pg = ["h","e","o"];
for (var i = 0; i < pg.length; i++) {
    $("#nav li a.pg[i]").mouseover(
      function () {
        if($(".content.pg[i]").is(":hidden")) {
            $(".content.pg[i]").fadeIn('fast');
        }
      }
    );
}
  • 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-25T22:07:35+00:00Added an answer on May 25, 2026 at 10:07 pm

    pg[i] inside quotes will not work as a variable, you need to append it to the selector string using +.

    More optimizations: The $.each method is fast and good to use when looping, and you don’t need to check if elem.is(':hidden'), just include :hidden in the selector:

    $.each(["h","e","o"], function(i, id) {
        $("#nav li a."+id).mouseover(function() {
            $(".content."+id+":hidden").fadeIn('fast');
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following: $('#EID').empty(); $('#EID').html(data); Is it possible to simplify this with jQuery
I've recently tried to simplify some of my code by using generics where possible.
In Perl, I can do this: push(@{$h->[x]}, y); Can I simplify the following python
Lets look at the following structure first (I have tried to simplify it to
Simplify the following xPath so that it is as small as possible but still
I'm gonna try and simplify this as much as possible. Lets say i have
Is it possible to simplify this code into a cleaner/faster form? StringBuilder builder =
Possible Duplicate: Breaking out of a nested loop I got somthing like (to simplify)
I would like to know if it is possible to simplify the following process:
I'll simplify the problem as much as possible: I have an oracle table: row_priority,

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.