afmoro
afmoro

Reputation: 11

Need to display result of addition in a text box using javascript

I need to create two text boxes where the user can insert number, and a button that adds them.

I need to use the .val() to bring the data from the html body, that is done.

My issue is that I don't know how to display the result in a text box, and not using the alert option.

Help!

Upvotes: 0

Views: 74

Answers (1)

Peege151
Peege151

Reputation: 1560

You need to add change listeners to your html elements and you need to run a function on change.

<input type='number' id='firstBox' onChange='calculate()' />
<input type='number' id='secondBox' onChange='calculate()' />

<input type='number' id='answerBox' />
function calculate(){
    var first = Number(document.getElementById('firstBox').value)
    var second = Number(document.getElementById('secondBox').value)

    var answer = document.getElementById('answer')
    answer.value = first + second
}

Also, I created a quick fiddle for you here, definitely check it out it will help you get acquainted.

Upvotes: 2

Related Questions