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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:31:56+00:00 2026-05-18T12:31:56+00:00

I have the following code: <script type=text/javascript> $(function() { //div1 toggle function runEffect(){ var

  • 0

I have the following code:

<script type="text/javascript">  
$(function() {
    //div1 toggle
    function runEffect(){
        var options = {};
        //run the effect
        $("#div1_toggle").toggle("blind",options,350);
    };
    $("#div1").click(function() {
        runEffect();
        return false;
    });
});
</script>

Imagine the above code literally duplicated for a number of divs named div1, div2, div3, etc.

This is horrendously bad, and I would like to rewrite the code such that it applies to a div of any name to toggle a div of (equal name + _toggle).

Bonus: how could I still allow some of these divs to incorporate a different toggle speed (i.e. the 350ms specified above) while reducing redundancy?

  • 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-18T12:31:57+00:00Added an answer on May 18, 2026 at 12:31 pm

    A lot of the time, simplifying your javascript is best done by simplifying your HTML first.

    It would be best if you don’t use IDs for the Divs (you don’t want to have to keep increasing the ID number yourself do you?) but instead used classes (classes are generic and repeatable). This would work best if the “toggle div” is related to the “clicked div” somehow, for example, one is after the other. I would do it like this:

    <div class="clickable">
    </div>
    <div class="togglable">
    </div>
    ... rinse and repeat, as many times as you want...
    

    Then your script is simple:

    $(function () {
       $(".clickable").click(function () {
          $(this).next(".togglable").toggle("blind", {}, 350);
       });
    });
    

    As for some divs toggling at a different speed? Sure, just add another class:

    <div class="clickable">
    </div>
    <div class="fast togglable">
    </div>
    

    Now change the script to:

    $(function () {
       $(".clickable").click(function () {
          // A "normal" div
          $(this).next(".togglable:not(.fast)").toggle("blind", {}, 350);
          // A "fast" div
          $(this).next(".togglable.fast").toggle("blind", {}, 100); // New value
       });
    });
    

    There are other ways to do this. You can stick the “fast” class on the “clickable” div instead, then you’d have two different click handlers. You could have a “normal” and a “fast” class, so you don’t have to do :not(.fast) and can just do .normal instead. Hope this gives you some ideas…

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

Sidebar

Related Questions

I have the following code where the function codeaddress geocodes the text feild value
I have the following code which loads a xml file to populate a dropdown.
i have the following map of the world: <!-- copyright (c) 2009 Google inc.
I have a page with anchor tags that are being converted to JQuery UI
I have an embedded application running httpd for a web server. On the main
OK, I have an Apple-themed nav bar, and for some reason there is some
Little bit confused... I am trying to track mailto links being clicked, but constantly
I am trying to inject JQuery into a UIWebView of a page that I
Every browser I've observed creates a <head> element that's accessible in the DOM even
I am developing a third-party service embedded in websites as a JS snippet, that

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.