0

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.

2
  • What is undefined, and where are you calling function Button? Commented Jul 16, 2019 at 16:50
  • in this example i forgot to do onclick="button" Commented Jul 16, 2019 at 16:53

2 Answers 2

2

The value is only being captured once, before your function is called. You need something more like:

        var linksubmit = document.getElementById("linksubmit"); 
        function Button() {
            // Get the value each time the button is pressed, instead of reporting
            // an old, stale value (probably empty string/null/undefined.)
            alert(linksubmit.value);
        }
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Looks like I was a lil late hitting the submit button, kshetline is the same thing minus es6 syntax. Upvoting his for answering speed :D
Thank You So Much Chris You Are A Hero. You deserve an upvote too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.