0

I cannot get value of input, why? Thanks for your answer! This is the code:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <input id="fullname"/>
    <button id="button">button</button>
</body>
    <script>
        var name = document.getElementById("fullname").value;
        var btn = document.getElementById("button");
        btn.onclick = function(){alert(name)};
    </script>
</html>

1
  • Please post your code. Post it as text here, not as image links. Use { } in the SO editor to mark it as literal code. Commented Jul 31, 2015 at 16:41

1 Answer 1

1
  1. Script tag need to be in the body tag
  2. You need to get the value when you have entered it

Working plunk

<script>
    var btn = document.getElementById("button");

    btn.onclick = function(){
      var name = document.getElementById("fullname").value;
      alert(name);
    };
</script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.