javascript - Need help changing appendChild to replaceChild -


is there easy way change appendchild replacechild?

this of course continuously adds more ele. reason doesn't put value inside div or span, seems put below it.

var para = document.createelement("p"); var total = document.createtextnode(parsefloat((subt + tax).tofixed(2))) ;  para.appendchild(total);  document.getelementbyid("total_id").appendchild(para); 

its updating this:

<div class="prod_totals">order total: $<span id="total_id"></span></div> 

you can use innerhtml instead of appendchild

document.getelementbyid("total_id").innerhtml = parsefloat((subt + tax).tofixed(2)); 

because you're not inserting user input values inside total_id element , far question mentions, data not later passed server think you'll safe using innerhtml here. if reasons you'd still use replacechild this:

var para = document.createelement("p"); var total = document.createtextnode(parsefloat((subt + tax).tofixed(2))) ;  para.appendchild(total);  var existingtext=document.getelementbyid("total_id").queryselector('p'); if(existingtext){     document.getelementbyid("total_id").replacechild(existingtext, para); } else{     document.getelementbyid("total_id").appendchild(para); } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -