Bootstrap input field inside tooltip popover removed from output html
Asked Answered
D

4

13

Hello i`m using boostrap 4.3.1 and included popper 1.14.7.

Normally I can add input fields in the content of the popup/tooltip. I don`t since when, but at the moment when I put input field in the content then only the text is visible.

When I look in the source (compiled html) I can see that popper or bootstrap removed the input fields. Do I something wrong?

    var options = {
        html: true,
        // content: function(){ return $(".amountElec.popup").html();},
        placement: "bottom",
        container: "body"
    };
    
    $(function(){
        $('#manualinput').popover(options);
    })
<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">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.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>


<div id="manualinput" 
     data-container="body" 
     data-toggle="popover"  
     data-content="test <input name='test' type='text' value='2'>" 
     data-html="true" 
     data-placement="bottom">
     
     OPEN TOOLTUP
</div>
Dialyse answered 8/3, 2019 at 16:49 Comment(0)
D
3

I found the solution...

I my case add this to the javascript:

        var myDefaultWhiteList = $.fn.tooltip.Constructor.Default.whiteList;
        myDefaultWhiteList.input = [];

https://getbootstrap.com/docs/4.3/getting-started/javascript/#sanitizer

Dialyse answered 9/3, 2019 at 12:55 Comment(0)
M
22

It's even easier as you think:

Add

sanitize: false

as config option if you want to disable sanitize at all. If you just want to adapt the whitelist, you are right with your solution

https://github.com/twbs/bootstrap/blob/438e01b61c935409adca29cde3dbb66dd119eefd/js/src/tooltip.js#L472

Macintyre answered 18/4, 2019 at 9:55 Comment(2)
You saved my day!Presbyopia
Mine too! Especially since it isn't in the official bootstrap documentation.Conversant
D
3

I found the solution...

I my case add this to the javascript:

        var myDefaultWhiteList = $.fn.tooltip.Constructor.Default.whiteList;
        myDefaultWhiteList.input = [];

https://getbootstrap.com/docs/4.3/getting-started/javascript/#sanitizer

Dialyse answered 9/3, 2019 at 12:55 Comment(0)
D
1

After searching in the debug console I found somehting in the tooltip.js from bootstrap.

content = sanitizeHtml(content, this.config.whiteList, this.config.sanitizeFn)

  setElementContent($element, content) {
    if (typeof content === 'object' && (content.nodeType || content.jquery)) {
      // Content is a DOM node or a jQuery
      if (this.config.html) {
        if (!$(content).parent().is($element)) {
          $element.empty().append(content)
        }
      } else {
        $element.text($(content).text())
      }

      return
    }

    if (this.config.html) {
      if (this.config.sanitize) {
        content = sanitizeHtml(content, this.config.whiteList, this.config.sanitizeFn)
      }

      $element.html(content)
    } else {
      $element.text(content)
    }
  }

sanitizeHtml function removes the input fields :(.

Dialyse answered 9/3, 2019 at 12:44 Comment(0)
M
0

I just turned of sanitize by default (globally):

$.fn.tooltip.Constructor.DEFAULTS.sanitize = false;
$.fn.popover.Constructor.DEFAULTS.sanitize = false;

https://getbootstrap.com/docs/3.4/javascript/#default-settings

Maury answered 4/8, 2022 at 11:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.