97
<div class="container">
    <div class="row" style="padding-top: 240px;">
        <a href="#" class="btn btn-large btn-primary" rel="popover"
            data-content="<form><input type="text"/></form>"
            data-placement="top" data-original-title="Fill in form">Open form</a>
    </div>
</div>

JSfiddle

I'm guessing that I would store the form contents within a javascript function...

How do I contain a form within a bootstrap popover?

Community
  • 1
  • 1
user1438003
  • 6,603
  • 8
  • 30
  • 36

8 Answers8

278

I would put my form into the markup and not into some data tag. This is how it could work:

JS Code:

$('#popover').popover({ 
    html : true,
    title: function() {
      return $("#popover-head").html();
    },
    content: function() {
      return $("#popover-content").html();
    }
});

HTML Markup:

<a href="#" id="popover">the popover link</a>
<div id="popover-head" class="hide">
  some title
</div>
<div id="popover-content" class="hide">
  <!-- MyForm -->
</div>

Demo

Alternative Approaches:

X-Editable

You might want to take a look at X-Editable. A library that allows you to create editable elements on your page based on popovers.

X-Editable demo

Webcomponents

Mike Costello has released Bootstrap Web Components. This nifty library has a Popovers Component that lets you embed the form as markup:

<button id="popover-target" data-original-title="MyTitle" title="">Popover</button>

<bs-popover title="Popover with Title" for="popover-target">
  <!-- MyForm -->
</bs-popover>

Demo

HaNdTriX
  • 28,732
  • 11
  • 78
  • 85
  • 1
    Nice solution, I just put the title to `data-title="Some Title"` within the `` tag for not having an extra div... – pebbo Nov 07 '12 at 22:23
  • 1
    @HaNdTriX +10 for the way you have put it. – 2sb May 26 '13 at 22:44
  • Yeah, awesome answer. It has to be said. Time and again! Thanks a lot. – nasty pasty Nov 14 '13 at 02:29
  • yepp thats right. The reason for this is that bootstrap won't cache the popover. Bootstrap removes it from the DOM when it closes. This restriction is therefore a problem of bootstrap.popover itself and not of this little hack. – HaNdTriX Oct 22 '14 at 11:46
  • If you want to persist the data, try to listen for the `hide.bs.popover` Event and then replace the `.popover-content` with the content from the popover. That might work. – HaNdTriX Oct 22 '14 at 11:52
  • Great answer. But i have one more question. how can I show tooltip at the bottom. According to the your sample, you are showing the tooltip at the right place... ???? Please help.. – Mahmut EFE Dec 17 '14 at 09:53
  • Thanks it workd. I have one more questions. when I click the other components tool tip is not hireing. I mean . when I cliecked empty area I want to close the opened tool tip... what can I do for that. which approach must I follow. – Mahmut EFE Dec 17 '14 at 11:51
  • 1
    Please read: http://getbootstrap.com/javascript/#popovers You are looking for the `trigger` option. (`trigger: 'focus'`) – HaNdTriX Dec 17 '14 at 15:49
  • nice solution, but afaik `footer` can't be used which is defined in the html of your jsFiddle (which you don't resolve either) – FranBran Feb 02 '15 at 13:48
  • is it possible to **bind** the content to the scope? The issue I'm facing is, that expressions defined within the html passed to `content` is not updated by angular – FranBran Feb 03 '15 at 09:51
  • 1
    Why are you not using: http://angular-ui.github.io/bootstrap/#/popover? It should solve your problems. – HaNdTriX Feb 03 '15 at 09:55
  • `data='html'` just did it. Thank you! – Shimmy Weitzhandler Oct 07 '15 at 00:39
50

Either replace double quotes around type="text" within single quotes, Like:

"<form><input type='text'/></form>"

OR

Replace Double quotes wrapping data-content with single quote, Like:

data-content='<form><input type="text"/></form>'
Anonymous Coder
  • 556
  • 2
  • 16
AdityaParab
  • 7,024
  • 3
  • 27
  • 40
12
<a data-title="A Title" data-placement="top" data-html="true" data-content="<form><input type='text'/></form>" data-trigger="hover" rel="popover" class="btn btn-primary" id="test">Top popover</a>

just state data-html="true"

zainiafzan
  • 121
  • 1
  • 3
5

like this Working demo http://jsfiddle.net/7e2XU/21/show/# * Update: http://jsfiddle.net/kz5kjmbt/

 <div class="container">
    <div class="row" style="padding-top: 240px;"> <a href="#" class="btn btn-large btn-primary" rel="popover" data-content='
<form id="mainForm" name="mainForm" method="post" action="">
    <p>
        <label>Name :</label>
        <input type="text" id="txtName" name="txtName" />
    </p>
    <p>
        <label>Address 1 :</label>
        <input type="text" id="txtAddress" name="txtAddress" />
    </p>
    <p>
        <label>City :</label>
        <input type="text" id="txtCity" name="txtCity" />
    </p>
    <p>
        <input type="submit" name="Submit" value="Submit" />
    </p>
</form>
 data-placement="top" data-original-title="Fill in form">Open form</a>

    </div>
</div>

JavaScript code:

    $('a[rel=popover]').popover({
      html: 'true',
      placement: 'right'
    })

ScreenShot

working updated fiddle screenshot

Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • @DERIIIFranz Thanks its fixed now: http://jsfiddle.net/kz5kjmbt/ CDN gone missing, checkout updated link and screenshot. `:)` – Tats_innit Feb 02 '15 at 23:28
  • This solution works on current Chrome and answers the question. However, having complex markups within html attributes may be error-prone and add risks on maintainability and learnability of the code. – Ricardo Nov 28 '17 at 22:24
5

You can load the form from a hidden div element with the Bootstrap-provided hidden class.

<button class="btn btn-default" id="form-popover">Form popover</button>

<div class="hidden">
  <form id="form">
    <input type="text" class="form-control" />
  </form>
</div>

JavaScript:

$('#form-popover').popover({
    content: $('#form').parent().html(),
    html: true,
});
Fred
  • 12,086
  • 7
  • 60
  • 83
3

Or try this one

Second one including second hidden div content to hold the form working and test on fiddle http://jsfiddle.net/7e2XU/21/

<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
   <script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-tooltip.js"></script>
    <script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-popover.js"></script>


<div id="popover-content" style="display: none" >
    <div class="container" style="margin: 25px; ">
    <div class="row" style="padding-top: 240px;">
        <label id="sample">
            <form id="mainForm" name="mainForm" method="post" action="">
    <p>
        <label>Name :</label>
        <input type="text" id="txtName" name="txtName" />
    </p>
    <p>
        <label>Address 1 :</label>
        <input type="text" id="txtAddress" name="txtAddress" />
    </p>
    <p>
        <label>City :</label>
        <input type="text" id="txtCity" name="txtCity" />
    </p>
    <p>
        <input type="submit" name="Submit" value="Submit" />
    </p>
</form>

        </label>
    </div>
          </div> 
</div>

 <a href="#" style="margin: 40px 40px;" class="btn btn-large btn-primary" rel="popover" data-content='' data-placement="left" data-original-title="Fill in form">Open form</a>


<script>

$('a[rel=popover]').popover({
    html: 'true',
placement: 'right',
content : function() {
    return $('#popover-content').html();
}
})



</script>
ivan ivan
  • 31
  • 1
  • This is a nice solution but your jsfiddle link does not seem to correspond to the same implementation. You find this plunk I made that implements it correctly: http://plnkr.co/edit/IV2YbLCFSiYI0T1mXOLN?p=preview. – bernardn Nov 07 '13 at 10:02
2

A complete solution for anyone that might need it, I've used this with good results so far

JS:

$(".btn-popover-container").each(function() {
    var btn = $(this).children(".popover-btn");
    var titleContainer = $(this).children(".btn-popover-title");
    var contentContainer = $(this).children(".btn-popover-content");

    var title = $(titleContainer).html();
    var content = $(contentContainer).html();

    $(btn).popover({
        html: true,
        title: title,
        content: content,
        placement: 'right'
    });
});

HTML:

<div class="btn-popover-container">
    <button type="button" class="btn btn-link popover-btn">Button Name</button>
    <div class="btn-popover-title">
        Popover Title
    </div>
    <div class="btn-popover-content">
        <form>
          Or Other content..
        </form>
    </div>
</div>

CSS:

.btn-popover-container {
    display: inline-block;
}


.btn-popover-container .btn-popover-title, .btn-popover-container .btn-popover-content {
    display: none;
}
Sam
  • 2,771
  • 2
  • 28
  • 41
1

I work in WordPress a lot so use PHP.

My method is to contain my HTML in a PHP Variable, and then echo the variable in data-content.

$my-data-content = '<form><input type="text"/></form>';

along with

data-content='<?php echo $my-data-content; ?>' 
bbruman
  • 667
  • 4
  • 20