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

  • Home
  • SEARCH
  • 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 8150695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:08:23+00:00 2026-06-06T15:08:23+00:00

How would I make this code smaller? Maybe a toggle, but people were saying

  • 0

How would I make this code smaller? Maybe a toggle, but people were saying this was easily done in jQuery. But the problem is that I am not a fan of using jQuery for just one thing in my code.

function open() {
document.getElementById('message').style.display='block';
document.getElementById('fade').style.display='block';
}

function close() {
document.getElementById('message').style.display='none';
document.getElementById('fade').style.display='none';
}
  • 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-06T15:08:25+00:00Added an answer on June 6, 2026 at 3:08 pm

    DRY it up.

    var b='block',h='none',m='message',f='fade';
    function s(i,d){document.getElementById(i).style.display=d}
    function open(){s(m,b);s(f,b)}
    function close(){s(m,h);s(f,h)}
    

    With the whitespace and proper variable names (to be passed to a minifier), this looks like:

    var show = 'block', hide = 'none', message = 'message', fade = 'fade';
    
    function setStyle(id, display) {
      document.getElementById(id).style.display=display;
    }
    function open() {
      setStyle(message, show);
      setStyle(fade, show);
    }
    function close() {
      setStyle(message, hide);
      setStyle(fade, hide);
    }
    

    There are some best-practices which don’t relate to the question but are worth considering if your project grows beyond this trivial situation:

    • Use a minifier. My favorite is uglifyjs. This allows you to use meaningful variable names in your unminified code (like the second example). The minifier will output code more like (but probably even better than) the first example. Even with a minifier, keep thinking about what it can and cannot do – creating a private shortcut to a long public API like document.getElementById can aid the minification if you use that API frequently. Look at the minified code to make sure there isn’t something you can do to optimize it.
    • Separate your javascript into .js modules that are loaded separate from the page and asychrounously, if possible.
    • Manage all your static assets (like the .js modules) so they have a long cache timeout – use the Expires: http header. Then change their URLs when they actually change. This way, clients can cache them indefinitely until you change them & then the client will immediately fetch a new version.
    • Put discrete modules inside function wrappers, so that your variables don’t conflict with other pieces of code – either your own or 3rd party modules. If you want to make a variable public, do it explicitly: window.pubvar =
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It would be easy enough to make my own system for this, but I'm
Based on this , how would you make ignoring parameters more succint? var m
I would like to make qqplots through a list of dataframes. I wrote this
i would like to learn how to make PHP/CSS syntax generator. i assume this
$(#tab1).click(function(){ loadTab(1); $('div.HOMEtabdiv ul.HOMEtabs a').removeClass('selected'); $(this).addClass('selected'); }); Would it be possible to make the
Okay i have this problem with every page i make. im not sure what
I would like to write a JavaScript function that validates a zip code, by
I would like to make a backup of my project, but the folder now
I'm trying to find a way to make this smaller by using functions/variables in
I would make to make a small WYSIWYG editor similar to the one used

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.