Skip to content Skip to sidebar Skip to footer

How To Get User's Input Text Using Javascript?

I'm trying to design a square-root calculator using javascript. I want the user to write the number to be 'square-rooted' into an input area. After that I want to get what user typ

Solution 1:

You can get the square root by using

var num =  Math.sqrt(document.getElementById('sqrt').value);

Solution 2:

You can use the getElementById DOM API function, and the get the value using the property with the same name:

document.getElementById('sqrt').value

This yields the number from the field.

You should call this code snippet in the onclick handler of the button, as on page load it is empty! So:

functionnihai() {
    var x = document.getElementById('sqrt').value;
    ...
}

Post a Comment for "How To Get User's Input Text Using Javascript?"