AnswerBun.com

How do I write a script that alerts a input value given by a visitor?

I’m a beginner in javascript. I need to code a basic script that alerts the input value given by visitor.

Here is my code:

function alertFunction() {
  var x.value = document.getElementById("student-name");
  alert(x.value);
}
<table>
  <tr>
    <td><label for="name">Student Name: </label></td>
    <td><input type="text" id="student-name"></td>
  </tr>
  <tr>
    <td><input type="button" onclick="alertFunction()" value="Enter" /></td>
  </tr>
</table>

There is something wrong. Please write me the correct one. Thanks much. Have a great day.

Stack Overflow Asked by Mehmet Akif VARDAR on February 18, 2021

3 Answers

3 Answers

You're trying to do X.value = dom element. Do this instead

function alertFunction() {
  var x = document.getElementById("student-name").value;
  alert(x);
}
<table>
  <tr>
    <td><label for="name">Student Name: </label></td>
    <td><input type="text" id="student-name"></td>
  </tr>
  <tr>
    <td><input type="button" onclick="alertFunction()" value="Enter" /></td>
  </tr>
</table>

Correct answer by Vikrant on February 18, 2021

In Simple you can declare your button as below in td

<td><button onclick="alertFunction()">Enter</button></td>

And in Function use the below code to get the value

var x = document.getElementById("student-name").value;

Answered by Vivek Lakshmanan on February 18, 2021

You can use value property to get value of the element.

function alertFunction() {
   var x = document.getElementById("student-name").value;
   alert(x);
}

Answered by firatozcevahir on February 18, 2021

Add your own answers!

Related Questions

Recording using sox in c/c++

1  Asked on February 18, 2021

       

nodejs writing to JSON into a specific path

6  Asked on February 18, 2021 by c4llm3p3t3r

     

Unusual growth tempdb

0  Asked on February 18, 2021 by pablo-sanchez

       

Delphi Parse JSON

2  Asked on February 17, 2021 by stanley-mladenek

   

How to create a filter using bootstrap and jQuery

2  Asked on February 17, 2021 by jefin-winston

     

Change Canvas sizes with zoom

0  Asked on February 17, 2021 by reit-abdullah-yavuzkol

 

Is there a way to make the Loop Code make it faster?

4  Asked on February 17, 2021 by levesque-xylia

 

Using libcurl in g++

0  Asked on February 16, 2021 by marcel-kopera

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.