javascript - jQuery replacewith varibles not getting replaced on change -


on dropdown change variable val not getting changed in replacewith. still set initial value not getting updated.

alert(val) give correct value. delete val not either.

$(document).ready(function() {   $("#answerdropdown").change(function() {       var val = $(this).val();       if (val != '') {         $("#filler").replacewith('<div id="replaced"> ' + val +  '</div>');         alert(val);         delete val;       }   }); }); 

fiddle

the problem after first change replacing div id='filler'

therefore 2nd , subsequent onchange there not element id='filler' - code doesn't run

you can instead

$("#filler").html('<div id="replaced"> ' + val +  '</div>'); 

this result in filler div having inner div

or

$("#filler").replacewith('<div id="filler"> ' + val +  '</div>'); 

which identical to

$("#filler").text(val); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -