简体   繁体   中英

get the text entered in input box using java-script

I'm new to javascript I'm trying to create a web page where we can enter name in an input box and using javascript's alert method show an alert box which says hello and the name that we entered in the input box my code is

    //html
<input id="test" type="text" name="nm" placeholder="Enter name">
<button onclick="fun(i dont know what to pass here in order to get the text entered in that text box)" type="button" name="button">Click here</button>

   //javascript
//i tried like this...first

function fun(x) {
  alert("HELLO" + x);
}

//i tried this...then
var x = document.getElementsById("test").value; [->here i also dono what to put.]

function fun(x) {
  alert("HELLO" + x);
}

It should be getElementById instead of getElementsById . I hope this helps, and happy learning :)

Your code:

 function fun() { var textInTest = document.getElementById("test").value; alert("Value entered" + textInTest); }
 <input id="test" type="text" name="nm" placeholder="Enter name"> <button onclick="fun()" type="button" name="button">Click here</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM