Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html form inside bootstrap popover not working

I'm trying to use a form inside a bootstrap popover. Some basic html works (text styling, buttons), but the form does not. (neither do onclick() javascript actions)

I tried several options, and could not find any solution online.

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>

    <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-html='true' data-content="
    <h3>This works</h3>
    <form>
    This does not:<br>
    <input type='text' name='firstname'><br>
    Last name:<br>
    <input type='text' name='lastname'>
    </form>
    ">popover with html form</button>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script>
        $(function () {
            $('[data-toggle="popover"]').popover({
                html: true
            })
        })
    </script>
</body>
</html>
like image 723
Joost Barendregt Avatar asked May 22 '19 20:05

Joost Barendregt


People also ask

How do I display popover in HTML?

To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.

How do I create a popover in bootstrap 5?

To create a popover, add the data-bs-toggle="popover" attribute to an element. Note: Popovers must be initialized with JavaScript to work.


1 Answers

Your HTML is just fine and works (see example below.) The issue is a new flag needs to be set to allow HTML in the popover for BS 4.3.1

Per their release notes for that version is a breaking change:

"Security: Fixed an XSS vulnerability (CVE-2019-8331) in our tooltip and popover plugins by implementing a new HTML sanitizer"

See implementation of sanitize below in your example:

$(function() {
  $('[data-toggle="popover"]').popover({
    html: true,
sanitize: false,
  })
})
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>




<button style="margin: 20px;" type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-html='true' data-content="
    <h3>This works</h3>
    <form>
    This does too:<br>
    <input type='text' name='firstname'><br>
    Last name:<br>
    <input type='text' name='lastname'>
    </form>
    ">popover with html form</button>


<button style="margin: 20px;" type="button" class="btn btn-lg btn-info" data-toggle="popover" data-html='true' data-content="
    <h3>This works</h3>
    <form>
    This does duece:<br>
    <input type='text' name='firstname'><br>
    Last name:<br>
    <input type='text' name='lastname'>
    </form>
    ">Duece</button>
like image 172
Travis Acton Avatar answered Nov 16 '22 12:11

Travis Acton