Ways to modify the background hue of a Bootstrap popover

I am trying to create 3 popovers with different background colors using Bootstrap and CSS. Ideally, I would like the background color to change based on the specific letter placed inside the span's class attribute.

<div class="container">
   <div>
     <span
           data-content="PopOver A"
           data-placement="right"
           data-trigger="hover"
           class="popoverjs A">PopOver A</span>
   </div>
   <div>
     <span
           data-content="PopOver B" 
           data-placement="right" 
           data-trigger="hover"
           class="popoverjs B">PopOver B</span>
   </div>
   <div>
     <span
           data-content="PopOver C" 
           data-placement="right" 
           data-trigger="hover"
           class="popoverjs C">PopOver C</span>
    </div>
</div>

<script>
$('.popoverjs').popover();
</script>

<style>

.popover-title {
  display: none; 
}

.popover {
  border: 0px;
  border-radius: 15;
  background: #39A5DB;
  color: white;
}

.popover.right .arrow:after {
  border-right-color: #39A5DB;
}

.A + .popover {
   background-color: blue;
 }

.B + .popover {
   background-color: green;
 }
 
 .C + .popover {
   background-color: red;
 }
 
</style>

However, for some reason, it is not working as expected. Any assistance or guidance you can provide would be greatly appreciated. To test the code yourself, visit this LINK TO JSFIDDLE

Answer №1

The new feature, the popover, is dynamically attached to the body and removed when the mouse is moved away from it. This behavior caused your CSS selectors to not work as expected. To solve this issue, you can apply a specific class to the body whenever a popover trigger element is hovered over and remove it on mouseout. This class can be defined using a data attribute. Subsequently, you can utilize CSS selectors to target the popover class when the body has the specified class.

To see a demo of this in action, visit: http://jsfiddle.net/L5q1etjc/

$('.popoverjs').popover().hover(function(){
    $('body').addClass($(this).data('addclass'));
}, function(){
    $('body').removeClass($(this).data('addclass'));
});
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css');

.container{
  margin-top: 10px;
}

.popover-title {
  display: none; 
}

.popover {
  border: 0px;
  border-radius: 15;
  background: #39A5DB;
  color: white;
}

.popover.right .arrow:after {
  border-right-color: #39A5DB;
}

body.A > .popover{ /*Change popover color*/
   background-color: blue;
 }
 
 body.A > .popover > .arrow, body.A > .popover > .arrow:after {
   border-right-color: blue; /*Change arrow color*/
 }

body.B > .popover {
   background-color: green;
 }
 
 body.B > .popover > .arrow, body.B > .popover > .arrow:after {
   border-right-color: green;
 }
 
 body.C > .popover {
   background-color: red;
 }
 
 body.C > .popover > .arrow, body.C > .popover > .arrow:after {
  border-right-color: red;
 }
 
 
<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.0/bootstrap.min.js"></script>
<div class="container">
   <div>
     <span
           data-content="PopOver A"
           data-placement="right"
           data-trigger="hover"
           data-addclass="A"
           class="popoverjs A">PopOver A</span>
   </div>
   <div>
     <span
           data-content="PopOver B" 
           data-placement="right" 
           data-trigger="hover"
           data-addclass="B"
           class="popoverjs B">PopOver B</span>
   </div>
   <div>
     <span
           data-content="PopOver C" 
           data-placement="right" 
           data-trigger="hover"
           data-addclass="C"
           class="popoverjs C">PopOver C</span>
    </div>
</div>

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

What is the best way to incorporate CSS from node_modules into Vite for production?

I have a TypeScript web application where I need to include CSS files from NPM dependencies in index.html. Here is an example of how it is done: <link rel="stylesheet" type="text/css" href="./node_modules/notyf/notyf.min.css&quo ...

Adapting the position of a table row in AngularJS based on the

I need assistance with creating a dynamic table-row that moves to indicate the current time in a table filled with timestamps. <table> <tr ng-repeat="timestamp in timestampArray"> <td>{{timestamp}}</td> </tr> ...

What is the reason for the absence of styles in index.html when accessing the local server?

When using node.js to open the server, I encountered an issue where the styles are not displaying. Strangely, when opening index.html directly in the browser, all the styles show up correctly. What could be causing this discrepancy? index.html <!doctyp ...

stylized tables with assorted colored categories

Here is the layout of my table: Grade Sign StudentGrade 6 + 1 6 0 6 - 0 5 + 0 5 0 5 - 0 4 + 0 4 0 4 - 1 3 + 0 3 ...

Managing multiple dropdown menus in PHP when dealing with Select options

In the given script, I am attempting to manage the selection between a dropdown option and the value of a text field: <script> function check() { var dropdown = document.getElementById("OpType"); var current_value = dropdown.options[dropdown ...

One way to view inherited width while ensuring that data remains inside a pre tag is by utilizing CSS

Currently, I'm facing an issue with displaying PHP and Javascript code within pre tags in a project. The problem arises as the code spills outside the boundary of the containing element. While inspecting the console, I am attempting to identify the so ...

Bulma: Tips for creating equally sized buttons?

I am working with Bulma CSS and trying to ensure that all my buttons are the same size. Currently, each button appears to have a different size based on the text content. I have looked into using the "is-fullwidth" option, but it makes the buttons too la ...

Tips for assigning a unique identifier to an HTML tag using JavaScript

When appending multiple items in JavaScript like this: data.context = $('<button class="btn btn-primary"/>').text('Upload') .appendTo('#fileTableId') .click(function () { ...

What is the best way to position my Jchartfx area graph below my gridview?

When my page loads, the graph appears like this. It consistently shows up in the top left corner even though it should be positioned underneath the grid view as intended. var chart1; function createGraph(mpy) { if (mpy == undefined) mpy = 12.00; ch ...

"Creating a custom navigation bar with full width and navigation corners on both left

I am struggling to make my website's navbar stretch to the edges of the container while maintaining full width. I have added left and right padding to each navigation item, but in smaller laptop resolutions, the items break onto a new line, which is n ...

Lint error: Unrecognized attribute 'text-fill-color' in CSS (unknown properties)

My navigation bar isn't functioning, and I can't figure out why. It seems like it might be related to this snippet of code: -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; text-fill-color: trans ...

My objective is to use JavaScript to dynamically update information in an HTML document using data retrieved from a JSON file

Experience the benefits of our membership program: PROGRAMMING var benefitsData = document.getElementById("memberBenefits"); for (var i = 0; i < membershipData.benefits.length; i++){ benefitsData[i].getElementsByTagName('h4')[0].innerHTML = ...

Large background images on websites often appear pixelated

Check out my website: I've noticed that the background image appears blurry on my screen, despite its size being 1920 x 1080 px. I've tried using a jquery script to manage this background issue as all of the CSS methods have provided unsatisfact ...

Preserving distance between absolute elements

I'm facing an issue with my menu animation using jQuery. In order to achieve the desired effect, I am forced to use position: absolute. My challenge is maintaining consistent spacing between each text string, especially since they vary in width. Unfor ...

What is the best method for calculating income and expenses within a JavaScript expense tracker?

I'm facing an issue where I can't add the value of a checked radio button to an object in a different function. Even though there are no errors, nothing gets added. My project involves creating an expense tracker, and adding either income or exp ...

Having trouble getting ASPJAX to function properly

I'm looking to create a demo showcasing the combination of ASP and AJAX. I came across some code snippets from http://www.aspjax.com that I've incorporated into my project. However, I'm having trouble displaying the expected text. Here is t ...

Right-align each item when selecting none

Is there a way to change the style of just one of the elements select or option, without affecting the style of both? I attempted to align the select element to the right, while leaving the option elements aligned to the left. However, this did not work a ...

Unusual actions observed when using CSS pseudo selector :checked

I have implemented a unique CSS solution to showcase a five-star rating system on my website. The method I followed can be found at . While this technique works flawlessly in some sections of my website, it strangely fails in others. Could this be due to c ...

Harnessing the power of external Javascript functions within an Angular 2 template

Within the component, I have a template containing 4 div tags. The goal is to use a JavaScript function named changeValue() to update the content of the first div from 1 to Yes!. Since I am new to TypeScript and Angular 2, I am unsure how to establish comm ...

When the window is scrolled to 50%, I would like to load some additional data

Looking to load data when the browser scrollbar reaches 50%... In jQuery, I created the following function: $(window).on('scroll', function () { alert("scrolling"); if ($(window).scrollTop() + $(window).innerHeight() > ...