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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:04:22+00:00 2026-06-09T01:04:22+00:00

How go get an input text value in JavaScript? <script language=javascript type=text/javascript> lol =

  • 0

How go get an input text value in JavaScript?

<script language="javascript" type="text/javascript">
    lol = document.getElementById('lolz').value;
    function kk(){
    alert(lol);
    }
</script>

<body>
    <input type="text" name="enter" class="enter" value="" id="lolz"/>
    <input type="button" value="click" OnClick="kk()"/>
</body>

When I put lol = document.getElementById('lolz').value; outside of the function kk(), like shown above, it doesn’t work, but when I put it inside, it works. Can anyone tell me why?

  • 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-09T01:04:23+00:00Added an answer on June 9, 2026 at 1:04 am

    The reason you function doesn’t work when lol is defined outside it, is because the DOM isn’t loaded yet when the JavaScript is first run. Because of that, getElementById will return null (see MDN).

    You’ve already found the most obvious solution: by calling getElementById inside the function, the DOM will be loaded and ready by the time the function is called, and the element will be found like you expect it to.

    There are a few other solutions. One is to wait until the entire document is loaded, like this:

    <script type="text/javascript">
        var lolz;
        function onload() { 
            lolz = document.getElementById('lolz');
        }
        function kk(){
            alert(lolz.value);
        }
    </script>
    
    <body onload="onload();">
        <input type="text" name="enter" class="enter" value="" id="lolz"/>
        <input type="button" value="click" onclick="kk();"/>
    </body>
    

    Note the onload attribute of the <body> tag. (On a side note: the language attribute of the <script> tag is deprecated. Don’t use it.)

    There is, however, a problem with onload: it waits until everything (including images, etc.) is loaded.

    The other option is to wait until the DOM is ready (which is usually much earlier than onload). This can be done with “plain” JavaScript, but it’s much easier to use a DOM library like jQuery.

    For example:

    <script type="text/javascript">
        $(document).ready(function() {
            var lolz = $('#lolz');
            var kk = $('#kk');
            kk.click(function() {
                alert(lolz.val());
            });
        });
    </script>
    
    <body>
        <input type="text" name="enter" class="enter" value="" id="lolz"/>
        <input type="button" value="click" id="kk" />
    </body>
    

    jQuery’s .ready() takes a function as an argument. The function will be run as soon as the DOM is ready. This second example also uses .click() to bind kk’s onclick handler, instead of doing that inline in the HTML.

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

Sidebar

Related Questions

<script language='javascript' type='text/javascript' charset='<?=$page_charset?>'> $(document).ready(function(){ $('#btn_login').click(function(){ $.search_keyword(); }); }); here is the script <form
I am trying to get the value of the text in the input fields.
I have below html code <div id=divTest> Hello <input type=text id=txtID value= /> <input
I am trying to get a input text area and a submit button attached
I am trying to get text input from the keyboard in Java 6. I
I have this input text and input button, I am trying to get them
I'm trying to get edit text to behave in this way: User can input
I'm trying to change the value of a text input field based on user
Hi I am trying to work on a script to get back XML text
I have JSF code like: <h:inputText id=from value=#{fromToMBean.fromName}/> I would like to get this

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.