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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:27:42+00:00 2026-06-06T11:27:42+00:00

I have used a javascript to show div by onclick but when i click

  • 0

I have used a javascript to show div by onclick but when i click outside div i want to hide the div.

after more searches, i have found an function, and it works perfectly

but there is an issue, the code requires double click for first time, to show the dive

my code:

<script>
// click on the div
function toggle( e, id ) {
  var el = document.getElementById(id);
  el.style.display = ( el.style.display == 'none' ) ? 'block' : 'none';

  // save it for hiding
  toggle.el = el;

  // stop the event right here
  if ( e.stopPropagation )
    e.stopPropagation();
  e.cancelBubble = true;
  return false;
}

// click outside the div
document.onclick = function() {
  if ( toggle.el ) {
    toggle.el.style.display = 'none';
  }
}

</script>
<style>#tools{display:none;}</style>

<div id="tools">Hidden div</div>
<a href="#" onclick="toggle(event, 'tools');">show/hide</a>

This is my full code, you can test it on your computer.

The question is: The function requires two clicks, i want show the div on click ( one click ) Not ( Double Click )

  • 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-06T11:27:45+00:00Added an answer on June 6, 2026 at 11:27 am

    Try this – http://jsfiddle.net/JjChY/1/

    Change

    el.style.display = ( el.style.display == 'none' ) ? 'block' : 'none';
    

    to

    el.style.display = (el.style.display == 'none' || el.style.display == '') ? 'block' : 'none';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div and I want to show and hide on a link
I have html that looks something like this: <div> <table> <tr onclick=show();> <td class=cell>Click
I have not really used JavaScript before but I am trying to validate form
I have used Javascript onlaod like this: function check() { var pic = new
I have dealt and used JavaScript and am quite comfortable with it. However I
I'm struggling with some JavaScript code that I have used countless times across my
I'm working on a security project in Javascript (something I honestly have not used),
I have this JavaScript which is used for creating shadow into input field: <!--
I have one javascript function that used to display the menu and content in
I have the following JavaScript Regex As used in http://regexpal.com/ \[.*(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*\] As use in

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.