LearnFast.Ninja

JavaScript alert message box function get variable from input text

Write JavaScript function who get element id as parameter then get the value of this element. Write html input text and button then execute the function above when onclick event occur. Try our working example.

Step 1: Write JavaScript function who get element id as parameter then get the value of this element. 

<script type="text/javascript">

    function Show_Alert(Input_Id) {

        var My_Message = document.getElementById(Input_Id).value;

        alert('Hello, ' + My_Message);

    }

</script>


Step 2: Write html input text and button then execute the function above when onclick event occur. 

<body>

    Enter your name:

    <br />

    <input type="text" id="My_Input_Text" />

    <br />

    <input type="button" value="Click Here" onclick="Show_Alert('My_Input_Text')" />

</body>


Working example:

<html>

<head>

    <title>Alert Box</title>

    <script type="text/javascript">

        function Show_Alert(Input_Id) {

            var My_Message = document.getElementById(Input_Id).value;

            alert('Hello, ' + My_Message);

        }

    </script>

</head>

<body>

    Enter your name:

 <br />

    <input type="text" id="My_Input_Text" />

    <br />

    <input type="button" value="Click Here" onclick="Show_Alert('My_Input_Text')" />

</body>

</html>


Result: 


Tags

javascript

alert message-box input