0

I have a delete button that when clicked triggers a popover. In that popover I have the real link doing the deleting. I want to select the id of the link inside that data-content(Bootstrap) of of the popover.

I have multiple elements on a page that could be deleted so I don't know their IDs beforehand but they have the same class, so I generate an ID, then use $(this).attr('id') to select the particular delete link.

This is the popover button and the link inside the data-content

<button tabindex="0" role="button" class="btn btn-danger btn-sm" data-toggle="popover"           
   data-placement="top" data-html="true" data-content="<a class='btn delete-draw text-danger' 
   id='[generatedId]'>Delete Draw</a>">Delete Draw
</button>

Now this is the jquery code

$(".delete-draw").click(function () {
    event.preventDefault();
    var drawId = $(this).attr("id");
    // make ajax delete call
 });

If I move the id and .delete-draw class to the outer popover button, it works well. So I'm thinking it has to do with the id of the a tag being nested in another data-attribute

Richard
  • 1,702
  • 2
  • 11
  • 17

2 Answers2

1

Since that anchor tag markup is added to the page after DOM Ready event here is how you would select that tag:

$(document).on('click', '.delete-draw', function() {
    let id = this.id;
    console.log( id );
});
PeterKA
  • 24,158
  • 5
  • 26
  • 48
0

You can passed the custom attribute inside $() and then access the id value.

Demo Code :

$(".btn-sm").click(function() {
  event.preventDefault();
  //getting custom attribute
  var drawId = $(this).attr("data-content");
  //selecting id using that elemnt
  var ids =$(drawId).attr('id')
  console.log(ids)

 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<button tabindex="0" role="button" class="btn btn-danger btn-sm" data-toggle="popover" data-placement="top" data-html="true" data-content="<a class='btn delete-draw text-danger' 
   id='1324'>Delete Draw</a>">Delete Draw
</button>
Swati
  • 28,069
  • 4
  • 21
  • 41