Search code examples
javascripthtmlgetelementbyid

How to Process textbox value to variable in JavaScript


I want to be able to type something into a textbox and then get an alert saying what I typed. Easy Right? I tried using getElementbyId and made some variables to accomodate with this, but it turns out it gives undefined. This is the code:

    <input type="submit" name="button" style="position: absolute; top: 283.5px; left: 45%; width: 142px; height: 40px; background-color:lime; border-color:forestgreen; font-weight:700; font-family:'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif" value="CLICK ME"/>
    <div style="position: absolute; top: 283.5px; left: 23%; width: 142px; height: 40px; background-color:lime; border-color:forestgreen;">
        <input type="text" name="linksubmit" id="linksubmit" style="position: absolute; top: 10px; width:138px" />
    </div>
    <script>
        var linksubmit = document.getElementById("linksubmit").value 
        function Button() {
            alert(linksubmit)
        }
    </script>

If any of you have a valid reason why this could not be working, it would be greatly appreciated.


Solution

  • const theThing = document.getElementById('testerooni');
    showMe = () => { alert(theThing.value) };
    <input id="testerooni" type="text">
    <button onclick="showMe()">SHOW ME</button>