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; } }); });
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
Post a Comment