Exploring the positioning, placement, and orientation of Bootstrap popovers

Is there a way to prevent the bootstrap popover from moving away from the triggering element when the window is resized?

What CSS should be used to position the tip of the popover near the top corner instead of on the left?

Despite setting the placement as 'auto right', the popover is still appearing on the left side. Any insights on how to fix this?

$(function () {
  $('.js-tooltip-trigger').popover({
    html: true,
    trigger: 'focus',
    placement: 'auto right',
    content: function (e) {
        return $(this).parent(".form-group").find(".js-tooltip").html();
    }
  }).on('shown.bs.popover', function () {
       $('.popover').css('top',parseInt($('.popover').css('top')) + 22 + 'px')
     });
});  

The HTML:

<div class="form-group">
<label for="ref">Reference</label>

<input type="text" class="form-control js-tooltip-trigger" id="ref" maxlength="50" >  

<div class="js-tooltip" style="display: none;">
    <p><strong>Your reference is optional.</strong></p>
    <p>Enter a code of your choice for reference purposes.</p>
</div>
</div>

Answer №1

This technique may not be everyone's favorite, but the concept involves creating an element that remains in a specific position to act as a reference for the popover. The popover is then appended to this designated element. In this scenario, I have utilized the existing container meant for tooltip HTML and dynamically generated container IDs to streamline the process.

Consider the following HTML structure:

<div class="form-group pos-relative">
    <label for="ref">Reference</label>
    <input type="text" class="form-control js-tooltip-trigger" id="ref" maxlength="50">
    <span class="js-tooltip" style="display: none;">
        <p><strong>Your reference is optional.</strong></p>
        <p>Enter a code of your choice for reference purposes.</p>
    </span>
</div>

The corresponding CSS would look like this:

.pos-relative{
  position:relative;
}
.js-tooltip{
   position:absolute; 
   top:0;
   right:0;
}

When it comes to JavaScript, the magic happens here:

$(function () {

  $('.js-tooltip-trigger').each(function(ind, ele){

    var $ele = $(ele),
        $ttSpan = $ele.next('.js-tooltip'),
        ttHtml = $ttSpan.html(),
        rndID = 'ttid'+ String(Math.random()).substr(2);

    // set the ID, strip the style, and empty the html--
    // we already have it stored in the var above
    $ttSpan.attr('id', rndID).removeAttr('style').html('');

    // create the popovers
    $ele.popover({
        html: true,
        trigger: 'focus',
        placement: 'right',
        container: '#'+rndID, 
        content: ttHtml
    });

  });

});

Experience the functionality here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customize the select option in HTML to hide the default arrow and provide additional spacing for the options

I am dealing with a select option markup that looks like this <div class="styleselect"> <select onchange="setLocation(this.value)" class="selections"> <option selected="selected" value="position">Position</option> <opt ...

stacking order of floated and positioned elements

After researching on MDN and reading through this insightful blog post, it is suggested that in the absence of a z-index, positioned elements are supposed to stack above float elements when they overlap. However, upon closer examination, the example prov ...

Achieving the minimum width of a table column in Material-UI

Currently I am in the process of developing a React website with Material-UI. One query that has come up is whether it's feasible to adjust the column width of a table to perfectly fit the data, along with some extra padding on both ends? Outlined be ...

What is the best way to increase padding beyond p-5 for specific screen sizes?

I need help creating a webpage that is responsive in design. Below is a snippet of the specific section I am working on: <div class="container-fluid"> <div class="row p-3 p-lg-5"> <div class="col"&g ...

Encountering problem when trying to upload several images at once using a single input in CodeIgniter with

I'm attempting to use CodeIgniter and AJAX to upload multiple images using a single input field. Here's the code I have so far: HTML: <input type="file" name="files[]" id="file" multiple /> AJAX: $("#addItems").on("submit",function(e) { ...

Having trouble invoking the "done" function in JQuery following a POST request

I am currently working on a Typescript project that utilizes JQuery, specifically for uploading a form with a file using the JQuery Form Plugin. However, after the upload process, there seems to be an issue when trying to call the "done" function from JQue ...

Exploring the functionality of CSS class selectors (.class-name) when used alongside CSS tag selectors (div, etc...)

I have designed my website with three distinct tables, each requiring unique styling. The general approach I take to apply styling to these tables is as follows: import './gameview.css' <div className="game-results"> <h ...

Adding items to an array retrieved from JSON

My goal is to merge 3 different Facebook feeds into a single object called "item." Each item includes the post creation time and the JSON data itself. However, I'm facing an issue where only 5 items are displayed when I check the log, even though ther ...

Using jQuery to dynamically populate select options based on JSON data

I have been testing and searching for a solution to my issue, but it still doesn't work. Any help would be greatly appreciated. I have three select options implemented in PHP like this: <div class="control-group" id="merkPrinter"> <label cl ...

similar element with alternate background shade

I have a custom component that I am using multiple times on a Vue page. I want to customize the background color for each instance of this component. Here is the code for my component: <template> <div class="project-card" :style="{backgroundCo ...

An easy guide to accessing a data attribute using the .on function!

In my quest to retrieve the HTML data attribute labeled productId, I attempted the following code snippet: $('body').on("click", '.myButton', function () { var productId = (this).dataset.productId; }); I have multiple but ...

Creating a JSON Response Using PHP API

I created a basic JSON response to test the functionality of an AJAX request on a mobile device. Using a local domain called test.local, I generated a json response. header("Content-Type:application/json; charset=utf-8"); echo json_encode(array('nam ...

What is the best way to personalize Material UI elements, such as getting rid of the blue outline on the Select component?

Embarking on my journey of developing a React app, I made the decision to incorporate Material UI for its array of pre-built components. However, delving into the customization of these components and their styles has proven to be quite challenging for me ...

What is the method for extracting CSS class names and storing them in an array using PHP?

Hey there, I have a bunch of CSS code and I'm looking for a way to extract only the names of the CSS classes without the unnecessary characters and values, and then store them in an array using PHP. For Example: .dungarees { content: "\ef ...

Differences in SVG rendering between Chrome and Firefox

I am eager to create a diagram on my website using SVG. To ensure the diagram is displayed in full screen, I am utilizing availHeight and availWidth to determine the client screen's height and width, then adjust the scaling accordingly. My current sc ...

Insert a new, unique div onto an HTML webpage using JavaScript, ensuring no duplicates are created

Having an issue with this code snippet where nothing is being added. It seems like the variable result may not be taking a number or something else, but I'm not entirely sure why. $(document).ready(function () //document.getElementById(1).style. ...

Tips for aligning a row with both text and input fields in the center

I'm working on a design that includes text and input fields in rows, and I want everything to be centered. Should I wrap all the content in a div and center it as a whole, or should I center each row individually? Any suggestions? Here is the code sn ...

Adjust the baseline of text in <li> elements by shifting it 2 pixels upwards

My webpage menu is set up with inline "li" elements within a "ul". Each "li" has a colored border and contains an "a" tag. I'm looking to change the text color inside the "a" tag and move it 2px up when hovering over it without affecting the li border ...

Customize button design with data from API request

I am currently working on a project to showcase the status of multiple servers within a cluster. To achieve this, I have established a status route in my web service that returns 'ok' if the server is functioning correctly and an error message if ...

Storing JSON response data in jQuery cache according to the endpoint

I am currently working on a way to cache a JSON response in case the endpoint fails or takes too long to respond to the request. From a previous Stackoverflow question, I have come up with this function. However, there is an issue where the function alway ...