Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please i have a problem here in my work i have and input field and have a button that i use to create new input field with onclick event but my problem is how to add numbers in both input field.

What I have tried:

var count = 1;
function add(){
count++;
var form = document.getElementById('3030')
var divtest = document.createElement("div");

divtest.innerHTML = '<form id = "code_3030"><div id = "credit_unit" name = "myfile"><input type = "text" id = "unit4" placeholder = "Credit" onkeyup = "mul()"></div></form>';

form.appendChild(divtest)
}



function mul(){

var unit = document.getElementById('unit4').value,
output = document.getElementById('code4');

if(form.appendChild(divest) == true){
var ans = unit * divtest;
}else{
alert("work it out");
}

output.value = ans;
}
Posted
Updated 3-May-17 3:44am

1 solution

Your Add() function will create multiple div and inputs with same id

<div id = "credit_unit" name = "myfile">
<input type = "text" id = "unit4" placeholder = "Credit" onkeyup ="mul()">
</div>

<div id = "credit_unit" name = "myfile">
<input type = "text" id = "unit4" placeholder = "Credit" onkeyup ="mul()">
</div>


getElementById will return more value nodes, you need to loop or take the default or create a unique id. Also remove te spaces between the types in your code and use double quotes in your javascript instead of single.

document.getElementById("code4")[0].value;
 
Share this answer
 
Comments
alertfrancis 3-May-17 10:12am    
please help me with the code i don't understand how to do what you ask me to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900