1
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    user name:
    <input type="text" id="t1">
    <br>
    <button type="button" onClick="myFunction()">display</button>

    <script type="text/javascript">
        function myFunction() {
            var str;
            str = document.getElementById("t1");
            alert(str);
        }
    </script>

</body>
</html>

In the above program it is displaying [object] inside the alert box,I don't know why,I want to display what ever user input in the text box.help me..........

rajeev
  • 499
  • 1
  • 8
  • 17

5 Answers5

3

You're only getting the html element which is represented as an object, not the actual contents of the <input>. You need to explicitly get that content using .value:

   function myFunction() {
        var str;
        str = document.getElementById("t1").value;
        alert(str);
    }
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Change:

str = document.getElementById("t1")

to:

str = document.getElementById("t1").value;

jsFiddle example

document.getElementById("t1") refers to the element so you need to specify the property of the element you want. In this case, the value.

j08691
  • 204,283
  • 31
  • 260
  • 272
0

Your str variable contains a reference to the object itself, not the text inside. What you want is:

str = document.getElementById('t1').value;
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
0

You should use t1.value in the alert. t1 is the whole input element but you are only interested at what has been entered i.e. its value.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
-3

rather than alert(str), use console.log(str), then check browsers console

gwillie
  • 1,893
  • 1
  • 12
  • 14
  • While logging to console might be helpful for inspecting `str`, his problem is that `str` is `#t1`, not `#t1`'s value. – canon Sep 30 '13 at 13:19