-3

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 types in that input area with javascript. Here's my HTML:

<form>
    <input type="number" id = 'sqrt'>
</form>

When user types a number inside input area, my plan is to get this number with a DOM method inside my javascript file. But I couldn't figure how? Any help will be appreciated.

arkantos
  • 497
  • 3
  • 14

2 Answers2

0

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:

function nihai() {
    var x = document.getElementById('sqrt').value;
    ...
}
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
0

You can get the square root by using

var num =  Math.sqrt(document.getElementById('sqrt').value);
Akshat
  • 479
  • 4
  • 9
  • Thanks for your post. But I'm trying to write a function that does what Math.sqrt method does just for practice. – arkantos Jan 10 '15 at 12:04
  • @BarristanSelmy: implementing the `Math.sqrt` method in all its glory is in fact rather difficult. Do you need to do all of it, or just do something more basic like finding a rounded down square root for integers? – Qantas 94 Heavy Jan 10 '15 at 12:34
  • Yes i know it. I am just trying to do son exercise on what I've learnt so far. Nothing serious:) It can work with integers for now. But I'm trying to improve it in a way it will calculate numbers like 44,12. But I'm stuck to be clear. – arkantos Jan 10 '15 at 12:41