Home » JavaScript

JavaScript | Input value from the user using prompt

JavaScript Input value from the user using prompt: Here, we will read values from the user using prompt and print the input values.
Submitted by Pankaj Singh, on October 22, 2018

Example 1) Input name and print

Code (JS & HTML):

<!DOCTYPE html>

<HTML>
    <HEAD>
        <SCRIPT>
            var name = prompt("Enter Your name:");
            var msg = "Welcome "+name;
            //alert(msg);
            document.write(msg);
        </SCRIPT>
    </HEAD>
    <BODY>
    
    </BODY>
</HTML>

Output

JS | Input value example 1

Example 2) Input two numbers and find their sum

Code (JS & HTML):

<!DOCTYPE html>

<HTML>
    <HEAD>
        <SCRIPT>
            function Calculate(){
                var a=parseInt(document.getElementById("txta").value);
                var b=parseInt(document.getElementById("txtb").value);
                var c=a+b;
                document.getElementById("txtc").value=""+c;
            }
        </SCRIPT>
    </HEAD>
    <BODY>
        <h2>The Sum Program</h2>
        <hr />
        <table>
            <tr>
                <td>
                    <label>Enter A:</label>
                </td>
                <td>
                    <input type="text" name="txta" id="txta" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>Enter B:</label>
                </td>
                <td>
                    <input type="text" name="txtb" id="txtb" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <input type="button" value="Calculate" onclick="Calculate()" />
                </td>
            </tr>
             <tr>
                <td>
                    <label>Sum</label>
                </td>
                <td>
                    <input type="text" name="txtc" id="txtc" readonly />
                </td>
            </tr>
        </table>
    </BODY>
</HTML>

Output

JS | Input value example 2






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.