bootstrap popover button click event does not working using jquery
Asked Answered
B

3

5

I use the code for bootstrap popover as follows,

<a href="javascript:void(0);" class="btn btn-primary btn-sm" rel="popover"  data-placement="top"
                        data-original-title="<i class='fa fa-fw fa-users'></i> Enter New Investor Group"
                        data-content="<div class='smart-form'><div class='col-sm-10'><label class='input'><input type='text' name='NewInvestor' placeholder='Investor Group Name' id='NewInvestor' /></label></div><div class='col-sm-2'><button id='btntest' class='btn btn-primary btn-sm assignedGroup' type='button' data-target-id='NewInvestor' >ADD</button></div></div><div class='clearfix'></div><br/>"
                        data-html="true">Add</a>

I would like to trigger a event on button click in jquery. Code as follows,

$("#btntest").click(function () {
        alert("You click on button");
    });

JSFiddle

What would be the reason my code is not working?

I will appreciate your help in advanced.

Beamy answered 12/5, 2015 at 8:42 Comment(2)
Your HTML is invalid. You can't put markup directly in to attributes like that - you need to encode it.Cohere
@RoryMcCrossan my HTML is valid. have a look at documentation for bootstrap popover getbootstrap.com/javascript/#popoversBeamy
R
12

Assuming your code renders correctly, try the code below.

$(function(){
   $(document).on('click',"#btntest",function () {
      alert("test button clicked");
   });
});

Edited

Take a look at my JsFiddle and see if it works http://jsfiddle.net/jb5fexp3/

Riles answered 12/5, 2015 at 9:12 Comment(5)
Is the html being displayed on the page? Could you put it on jsFiddle so that I can check it?Riles
Updated my question with JSFiddle.Beamy
yeah checked that. So when you click on the "Add" button you need the click event to trigger right?Riles
Exactly, event should be trigger by clicking on Add button.Beamy
take a look at my jsFiddle on the Edited AnswerRiles
L
3

Check the Bootstrap documentation on popovers again:

<button type="button" class="btn btn-lg btn-danger" data-toggle="popover" title="Popover title" data-content="And here's some amazing content. It's very engaging. Right?">
    Click to toggle popover
</button>

and in your JS:

$(function () {
    $('[data-toggle="popover"]').popover()
})
Landlordism answered 12/5, 2015 at 8:57 Comment(0)
E
1
$(document).on("click", "#btntest", function () {
 // your code`enter code here`
})`;`

will bind the event on the #btntest elements which are not present at the time of binding event example using ajax html data set.

Exhibitive answered 4/10, 2018 at 11:17 Comment(2)
$(document).on("click", "#btntest, function () { will bind the event on the #btntest elements which are not present at the time of binding event example using ajax html data set.Exhibitive
this is usefull for meAttention

© 2022 - 2024 — McMap. All rights reserved.