javascript - jQuery append element and replace selected row -
how change elements of selected row?
when combo box changed, want check value of combo. if value of "gender" chosen, replace element "input value ke-3" textbox value :
<select id="data3" name="data3[]"> <option value="man" selected >man</option> <option value="woman">woman</option> </select>
whether can done using $.(selector).html() ?
my code :
$(document).ready(function() { mylogic(); }); function mylogic(){ $(".addcf").click(function(){ $("#customfields").append( '<tr>' + '<td>' + '<select class="tabelbaru" id="data1" name="data1[]">' + '<option value="email" selected >email</option>' + '<option value="gender">gender</option>' + '<option value="age">age</option>' + '</select> ' + '</td>' + '<td>' + '<input type="text" id="data2" name="data2[]" value="" placeholder="input value ke-2" />' + '</td>' + '<td>' + '<input type="text" id="data3" name="data3[]" value="" placeholder="input value ke-3" />' + '</td>' + '<td>' + '<a href="javascript:void(0);" class="remcf">remove</a>' + '</td>' + '</tr>' ); }); $("#customfields").on('click','.remcf',function(){ $(this).parent().parent().remove(); }); $(document.body).on('change', '.tabelbaru', function() { var nilai = $(this).val(); if(nilai=='email'){ $("#data3").html('change input type here...'); } }); }
how do that?
first of not use duplicate ids. use classes instead.
add change handler this:
$("#customfields").on('change', '.tabelbaru', function() { var $this = $(this), nilai = $this.val(); console.log(nilai); if(nilai=='gender'){ $this.closest("tr").find(".data3").replacewith('<select name="data3[]" class="data3"> <option value="man" selected >man</option> <option value="woman">woman</option></select>'); //creating select } else { $this.closest("tr").find(".data3").replacewith('<input type="text" name="data3[]" value="" placeholder="input value ke-3" class="data3"/>'); } });
Comments
Post a Comment