1

Please I want to output a variable from js to an html tag so i can see the results in a specific place on my page. here is the code:

<script type="text/javascript">
function doMath()
{
    // Capture the entered values of two input boxes
    var my_input1 = document.getElementById('my_input1').value;
    var my_input2 = document.getElementById('my_input2').value;
    var my_input3 = document.getElementById("my_input3").value;
    var my_input4 = document.getElementById("my_input4").value;
        //alert(form.elements["my_input2"].value);
    //var my_input3 = document.getElementById('my_input3').value;

    // Add them together and display
    var sum = parseInt(my_input1) / parseInt(my_input2) *     parseInt(my_input3) * parseInt(my_input4);
    document.write(sum);
}

1 Answer 1

3

Let us say you want to display the response in a div with id sum

<div id="sum"></div>

To do so, update

document.write(sum);

to

document.getElementById("sum").innerHTML = sum;
Sign up to request clarification or add additional context in comments.

5 Comments

@KofYeb - Great it worked for you. You can always mark the answer as accepted for future reference.
Okay. how do i pass that to an input field like this <div id="sum"></div> will now be <input type="text" name="PayableAmount" class="form-control" id="sum" value=""/> i want to know if its possible like that? and how do i do it the proper way. thanks and how do i accept answer? thanks
Update the code to document.getElementById("sum").value = sum;
like this <input type="text" name="PayableAmount" class="form-control" document.getElementById("sum").value = sum; /> i just tried this but its not workning
No no. Not like this. Your html will be <input type="text" name="PayableAmount" class="form-control" id="sum" value=""/> only. The script will change. In place document.write(sum); it will be document.getElementById("sum").value = sum;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.