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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:06:30+00:00 2026-06-10T07:06:30+00:00

document.getElementById(but).onclick = showDropDown; function showDropDown(e) { document.getElementById(but).onclick = function() {}; if (e.stopPropagation) e.stopPropagation(); //

  • 0
document.getElementById("but").onclick = showDropDown;

function showDropDown(e) {
    document.getElementById("but").onclick = function() {};
    if (e.stopPropagation) e.stopPropagation(); // W3C model
    else e.cancelBubble = true; // IE model
    document.getElementById("window").style.display = "inline-block";
    document.onclick = function(e) {
        var ele = document.elementFromPoint(e.clientX, e.clientY);
        if (ele == document.getElementById("but")) {
            hideDropDown();
            return;
        }
        do {
            if (ele == document.getElementById("window")) return;
        } while (ele = ele.parentNode);
        hideDropDown();
    };
}

function hideDropDown() {
    document.onclick = function() {};
    document.getElementById("window").style.display = "none";
    document.getElementById("but").onclick = showDropDown;
}​


<input id="but" type="button" value="pressMe" />
<div id="window" style="display:none">popup</div>

Demo: http://jsfiddle.net/nazym/

I was trying to make the JavaScript code dynamic using variables instead of the specified elements’ names but I could not. It always returns errors. I want to link it with different elements.

update
I want to replace the ids of the elements in the JavaScript code with variables so I can use it with any element.I tried to do it but failed. Basically, I want to use variables instead of the ids of the element and link it to the elements somehow again.

  • 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-10T07:06:31+00:00Added an answer on June 10, 2026 at 7:06 am

    Use arguments instead:

    function showDropDown(element, e) {
    
        element.onclick = function() {};
    
        // ....
    
        hideDropDown(element);
    
    }
    

    And you would give the element it’s onclick event handler like this:

    document.getElementById('but').onclick = function(event) {
        showDropDown(this, event);
    };
    

    Demo: http://jsfiddle.net/xNSZm/

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

Sidebar

Related Questions

document.getElementById(but).onclick = function(e) { showDropDown(this, e); }; function showDropDown(element, e) { element.onclick = function()
I have a js code: window.onload = function() { document.getElementById(Button1).onclick = function() { var
So for example I can do this: document.getElementById('element').onclick = function() { document.getElementById('element').style.color = '#FFFFFF';
Is there a reason why I can do this: document.getElementById('gridID').style.background='blue'; but when I try
I have this code: document.getElementById('img'+i).onclick = function(){popup_show('popup',array_msg[i]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict, 'AddEditSchedule;;popup_drag2;;EditSched;;'+String(array_msg_id[3])+';;view', 'popup_drag', 'popup_exit', 'screen-center', 0, 0);}; ...but
I have the javascript code for a link click: document.getElementById('giddy').onclick = function { alert(this.href);
<td><input type=button value=remove onClick=removeBox(this.parentNode.parentNode.rowIndex); /></td> function removeBox(i) { document.getElementById(firstTbl).deleteRow(i); } my this code is
I've got JavaScript code like this => (function(){ document.getElementById(element).onclick = function(){ var r =
<html> <head> <script type=text/javascript> function changepic() { document.getElementById(demo).style.top=2000; } </script> </head> <body> <h1>My First
I am trying to access the DIV with the id codeDef using document.getElementById['codeDef'] But

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.