skip to Main Content

I have these form inputs, where I have an add button which creates new rows.

My problem is the info button – which is a bootstrap popover. If I add more lines, and click the info button for the new row then the 1st info button opens not the info button for the new row.

How can I fix this?

I have tried this Jsfiddle

var dataList = $('.products');
var jsonOptions = [{
  "product": "11111",
  "description": "description 1",
  "labels": [{
    "version": "01",
    "quantity": 500
  }, {
    "version": "02",
    "quantity": 800
  }, ]
}, {
  "product": "22222",
  "description": "description 2",
  "labels": [{
    "version": "01",
    "quantity": 900
  }, {
    "version": "02",
    "quantity": 100
  }, ]
}, {
  "product": "33333",
  "description": "description 3",
  "labels": [{
    "version": "01",
    "quantity": 200
  }, {
    "version": "02",
    "quantity": 4300
  }, ]
}];

jsonOptions.forEach(function(item) {

  var option = '<option value="' + item.product + '">' + item.description + '</option>';

  dataList.append(option);
});

$(function() {
  $('body').on('input', '.product,.products', function() {

    var i = this.value;
    var description = "";
    var productsInBox = 0;
    var text = "";
    var version = '';
    var quantity = '';
    jsonOptions.forEach(function(a) {
      if (a.product == i) {
        description = a.description;
        productsInBox = a.productsInBox;
        text = a.labels
        a.labels.forEach(function(el) {
          version += el.version + " ";
          quantity += el.quantity + " ";
        });
      }
    });

    $('[data-toggle="popover"]').attr('data-content', "versions are: " + version + ' and quantity are: ' + quantity).data('bs.popover').setContent();
    $(this).closest('.form-group').find('.description').val(description);
    $(this).closest('.form-group').find('.mytext').val(description);


  });
});

var counter = 0;

$('#form1')
  .on('click', '.addButtonDED', function() {
    counter++;
    var $template = $('.form-group').slice(-1).clone(true, true).find('input').val('').end()
      .find('.addButtonDED').removeClass('addButtonDED').addClass('removeButtonDED').end()
      .find('[name^="product-"]').attr('name', 'product-' + counter).end()
      .find('[name^="description-"]').attr('name', 'description-' + counter).end()
      .find('i').removeClass('fa-plus').addClass('fa-minus').end();
    $template.insertAfter('.form-group:last');

  })

  // Remove button click handler
  .on('click', '.removeButtonDED', function() {
    var $row = $('.form-group').slice(-1);
    counter--;
    $row.remove();
  });

$('[data-toggle="popover"]').popover();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>



<form id="form1" method="post" class="form-horizontal" role="form">
  <fieldset>
    <div class="form-group">
      <div class="col-sm-2">
        <input type="text" list="products" class="form-control product" name="product[]" />
        <datalist id="products" class="products"></datalist>
      </div>

      <div class="col-sm-3">
        <input id="" type="text" class="form-control description" name=" description[]" />
      </div>

      <div class="col-sm-1">
        <button type="button" class="btn btn-default addButtonDED"><i class="fa fa-plus"></i></button>
      </div>

      <div class="col-sm-3" style="margin-left: 10px;">
        <button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like:  version 01 has 500 "><i class="fa fa-info"></i></button>
      </div>

    </div>
  </fieldset>
</form>

4

Answers


  1. you can use popover settings for define selector like:

    var popOverSettings = {
        placement: 'bottom',
        container: 'body',
        html: true,
        selector: '[rel="popover"]'
    };
    

    update fiddle here

    Login or Signup to reply.
  2. Instead of :

    $('[data-toggle="popover"]').popover();
    

    You should try this :

    $('[data-toggle="popover"]').each(function(){
        $(this).on("click", function(){
            $(this).popover();
        });
    });
    
    Login or Signup to reply.
  3. Change this :

    $('[data-toggle="popover"]').popover();

    To this:

    $(document).on("click", '[data-toggle="popover"]', function(){
        $(this).popover();
    });
    

    It requires double click tho to open the popover.

    Login or Signup to reply.
  4. Try replacing

    $('[data-toggle="popover"]').popover();
    

    with

    $('body').popover({
      selector: '[data-toggle="popover"]'
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search