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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:44:35+00:00 2026-06-13T05:44:35+00:00

I would like to assign document elements to global variables so that I am

  • 0

I would like to assign document elements to global variables so that I am able to use these elements everywhere in my code.

My code:

// document elements
var drop = null;
var status = null;
var show = null;

function process (drop, status, show) {
    if (document.readyState == 'complete') {
        // init elements
        drop = document.getElementById(drop);
        status = document.getElementById(status);
        show = document.getElementById(show);
    }

    // init event handlers
    drop.addEventListener('drop', handleDrop, false);
}

function handleDrop (evt) {
    // do something
}

The problem is I can’t do anything with the document elements using the global variables in the function handleDrop while in the function process everything works as it should…

EDIT: For example, I can read the content of the element show (show.innerHTML) in the function process but not in the function handleDrop.

  • 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-13T05:44:39+00:00Added an answer on June 13, 2026 at 5:44 am

    The problem is that all these drop, status… variables within process function are the local ones – they refer to its arguments, and not the variables defined earlier.

    If these were defined at the outer-most level (i.e., not within any function’s body), you can access (and alter) them like this:

    window.drop = document.getElementById(drop);
    window.status = document.getElementById(status);
    window.show = document.getElementById(show);
    

    But I’d actually suggest another way: using separate names for params and ‘closured-over’ variables. Like this:

    function process(dropId, statusId, showId) {
        if (document.readyState == 'complete') {
            // init elements
            drop = document.getElementById(dropId);
            status = document.getElementById(statusId);
            show = document.getElementById(showId);
        }
    }
    

    Both ways allow you to address these variables within handleDrop function; the latter one is obviously superior, because you are not restricted with what scope to choose.

    • 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. It works in an html document. I would like
I have a native JavaScript object, that I would like to assign the .ajaxSuccess
I have a column chart and I would like to be able to assign
I would like to assign a variable instead of the string in my following
I would like to assign a static list of items in a SelectList() to
I have a Customers table and would like to assign a Salesperson to each
I am fetching an array and I would like to assign attributes of items
I have a JQuery plugin function where I would like to assign selectors from
I would like to try to assign a variable value to input hidden field
Would like a for loop in jquery so that: For every hover_link: show hidden

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.