1
<div class="form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Add Skill</label>
  <div class="col-md-6 col-sm-3 col-xs-12">
    <input  type='text' id='others' name='others' class="form-control col-md-7 col-xs-3"/>
    <button type='submit' onclick='btn2' value='Add' class="btn btn-success"></button>
  </div>
</div>

This is my code i want that when i click on the button it makes a new chekbox

10
  • Not clear enough. Do you want to create checkboxes depending on the response from ajax? Commented Jan 9, 2017 at 6:05
  • please paste your JS or ajax code also or better make js fiddle Commented Jan 9, 2017 at 6:06
  • can you please write down your btn2 function? Commented Jan 9, 2017 at 6:06
  • So have you tried anything to achieve this? Commented Jan 9, 2017 at 6:10
  • i don't have a JS or ajax code nor btn2 function i don't know about ajax @BilalZafar John Christian Commented Jan 9, 2017 at 6:10

3 Answers 3

6

You have to add checkbox at runtime on success callback of your AJAX Request

Try this:

 $.ajax({
      url : 'fila path',
      type: 'POST',
      data: data,
      success : function(response){ 
           $('#list').append("<input type='checkbox' name='checkboxname'/><br>");
      }
  });
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use ajax success callback function to add the checkbox.

$(function() {
  $('#btn2').on('click', function() {
    // paste the below code in your success callback
    $('<input type="checkbox" name="chkItem" />').insertBefore(this);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Add Skill</label>
  <div class="col-md-6 col-sm-3 col-xs-12">
    <input type='text' id='others' name='others' class="form-control col-md-7 col-xs-3" />
    <button type='button' id='btn2' value='Add' class="btn btn-success">Add</button>
  </div>
</div>

Above snippet will help you to add checkbox. I've added the checkbox on a simple button click, you can modify it with your ajax code. Hope this will help you.

Updated with ajax code,

$(function() {
    $('#btn2').on('click', function() {
       var self=this;
       $.ajax({
           url:'your_server_url',
           type:'POST', // let it is POST method
           success:function(response){
              $('<input type="checkbox" name="chkItem" />')
                    .insertBefore(self); // insert before your clicked button
           }
       });
    });
});

8 Comments

OP wants the checkboxes to be created depending on ajax response.
when i write php in textbox, how will it become a name of that chekbox ?
@SougataBose Did you read "You need to use ajax success callback function to add the checkbox." in my answer.
Yes. But OP doesn't even know how to use ajax.
This is SO, only for helping in your code not a Freelance site(can you write a code for me).
|
1
$("#addCheckBox").on("click",function(){
    $.ajax({
            url : 'Url to get the ajax response',
            dataType: "json",
            method: 'post',
        data: {
           data: data,
        },
         success: function( response ) {
         $('#others').append("<input type='checkbox' name="checkboxCreated"/>");
        }
        });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.