satya satya - 1 year ago 58
HTML Question

Can not delete the radio button input field dynamically using Javascript/Jquery

I need one help. I am creating multiple text area,radio button field and drop down list by click on a plus button.Here i need when user will click on minus button the last all 3 type item should delete but its not happening like that. I am explaining my code below.

<input name="no_of_question" id="ques" class="form-control" placeholder="no of question" type="text">
<input type="button" class="btn btn-success btn-sm" name="plus" id="plus" value="+" onClick="addQuestionField();">
<input type="button" class="btn btn-danger btn-sm" name="minus" id="minus" value="-" onClick="deleteQuestionField();">
<div id="container">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<textarea class="form-control" name="questions0" id="questions0" placeholder="Questions" style="background:#FFFFFF;" rows="2"></textarea>
<div class="col-md-9">
<div style="margin-top:37px;">
<div style="float:left; margin-right:10px;">
<input type="radio" name="answer_type0" id="answer_type0" onClick="selectScale(this.value,'0');">
<div style="float:left; margin-top:-10px;display:none;" id="scaleid0">
<select class="form-control" id="nscale0" name="noofscale0">
<option value="">Select Answer Type</option>
<option value="1">Cuttuck</option>
<option value="1">BBSR</option>

The Javascript code is given below.

function addQuestionField(){
var get =$("#ques").val();
if(get===null || get===''){
alert('Please add no of questions');
for(var i=1;i<get;i++){
$('#container').append('<div class="row"><div class="col-md-3"><div class="form-group"><textarea class="form-control" name="questions'+ i +'" id="questions'+ i +'" placeholder="Questions" style="background:#FFFFFF;" rows="2"></textarea></div></div> <div class="col-md-9" ><div style="margin-top:37px;"><div style="float:left; margin-right:10px;"> <input type="radio" name="answer_type'+i+'" id="answer_type0" onClick="selectScale(this.value,'+i+');"></div><div style="float:left; margin-top:-10px;display:none;" id="scaleid'+i+'"><select class="form-control" id="nscale'+i+'" name="noofscale'+i+'"><option value="">Select Answer Type</option><?php foreach($feeddatascale as $v){ ?><option value="1" >Cuttuck</option><?php } ?></select></div><div style="clear:both;"></div></div></div><div>');
function deleteQuestionField(){
var get =$("#ques").val();
var textareas = $('#container textarea');

if (textareas.length !== 0) {

$('#ques').val(textareas.length - 1);

Here when i am clicking
button only the last text area is removing. But i need to delete the respective radio button and drop down list(
if exist
) with text area. here the total plunkr code is present. Please help me.

Answer Source

Add a wrapper class for the input set all together and remove it on delete,Always in templating, try to use wrapper class or id to create blocks of html code

$('#container').append('<div class="row dyn">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download