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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:08:58+00:00 2026-06-12T12:08:58+00:00

I’m making a very simple pop up where I can choose from 8 types

  • 0

I’m making a very simple pop up where I can choose from 8 types of content, all using the same format. It works by clicking on hidden divs that show on hover on the top section, of course as you can see I came up with a very long and large code for something that can probably be done with much less bolts and wires.

Since it’s a lot of lines I pasted all this in jsFiddle

Is there a way to make this lighter?

SOLVED… Yeees!

Thanks to all… here is the final script: jsFiddle Final in case someone else has the same difficulty

  • 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-06-12T12:08:59+00:00Added an answer on June 12, 2026 at 12:08 pm

    I see three things you could do to end up with less code:

    • Use the hover shortcut
    • Use anonymous functions
    • Use a parameterized function

    First, jQuery has the hover method to replace mouseover/mouseout behavior:

    $('#scrollbtnR8').bind('mouseover', R8);
    $('#scrollbtnR8').bind('mouseout', R8b);
    

    Can be replaced as

    $('#scrollbtnR8').hover(R8, R8b);
    

    That’s just a little less code though 🙂


    Second, you are now defining a function for each eventhandler but you are only using these functions once. If you only use them once, you could create anonymous functions like this:

    $('#scrollbtnR1').bind('click', esta1);
    

    becomes

    $('#scrollbtnR1').click(function() { 
        $("#scrollcontrol").animate({"left":-240},500, "swing", null);
    });
    

    Finally, you can use a function to encapsulate the common parts of the code and pass the changing parts as parameters.

    You could implement it like this:

    <div id="scrollcontrol" animate="swing" swingLeft="-240">
    
    $('div[animate='swing']').click(function() { 
        $(this).animate({"left": $(this).attr("swingLeft")}, 500, "swing", null);
    });
    

    The “div” selector uses the Attribute Equals Selector. You could also assign a class to all “animate” divs and select them using the class selector: $("div.animate") which would select all <div class="animate">.

    What happens here is:

    • Select all divs that have the value ‘swing’ for the animate attribute.
    • Call jQuery swing animate BUT use $(this).attr("swingLeft") as the left property.

    attr("wingLeft") gets the value for the swingLeft property as defined in your HTML markup.

    I stopped looking at your code at this point, the key is: Look at the code to be executed and see if you notice a pattern, something that you can generalize. You could then perhaps simplify the code further.
    Also worth noting: Some people don’t like adding properties like “animate”, “swingLeft” etc to the HTML. Update As per pimvdb’s comment, you can use the jQuery data to be a bit ‘cleaner’.

    The code could look like this then:

    <div id="scrollcontrol" class="swing" data-swingLeft="-240">
    
    $('.swing').click(function() { 
        $(this).animate({"left": $(this).data("swingLeft")}, 500, "swing", null);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the

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.