Guide on making an SVG tooltip using the Menucool JS extension

I am trying to add a simple tooltip to an SVG "map" when the mouse hovers over a rectangle.

To create the tooltip, I would like to utilize this specific plugin.

Here is how I have connected the SVG to HTML:

<object data="map.svg" type="image/svg+xml" id="map" width="1840" height="940"></object>

My initial attempt looked like this:

var svgobject = document.getElementById('map'); 
if ('contentDocument' in svgobject) {           
    var svgdom = $(svgobject.contentDocument);  
    $("#rect4578").tooltip.pop(this, '#ToltipContent');
}

Below is the code for the tooltip container:

<div style="display:none;">
    <div id="ToltipContent">
        <h2>Header</h2>
        <img src="img/img.jpg" style="float:right;" />
          Some text
    </div>
</div>

I made sure to add the 'tooltip' class to the rect4578 as instructed on the plugin website. However, it did not work.

Then I attempted to invoke the plugin inside the onmouseover attribute of the SVG rectangle.

onmouseover="tooltip.pop('#rect4578', '#ToltipContent')"

Unfortunately, even this method did not produce any results.

However, changing the opacity of the rectangle using either method described above did work.

So my question is, what is the correct way to implement this plugin for creating tooltips within an SVG?

Thank you.

Answer №1

Due to the unavailability of your object, I utilized an SVG from w3schools which proved to be effective. Here is the approach I took to ensure its functionality:

.hide {display:none;}
#object {width: 100px; overflow: hidden; margin: 0 auto;}
#object svg {padding: 20px;}
div#mcTooltip h2 {
    margin-top: 10px;
    line-height: 1;
}
#mcTooltip ul, #mcTooltip ol {
    padding-left: 20px;
}
div#mcTooltip {
    line-height: 16px;
    border-width: 1px;
    color: #333;
    border-color: #bbb;
    padding: 20px;
    font-size: 12px;
    font-family: Verdana, Arial;
    border-radius: 6px;
    box-shadow: 0 1px 4px #aaa;
}
div#mcTooltip, div.mcTooltipInner {
    background-color: #eee;
}
div#mcTooltip a {
    color: #069;
}
div#mcTooltip a:hover {
    color: #333;
}
div#mcTooltipWrapper {
    position: absolute;
    visibility: hidden;
    overflow: visible;
    z-index: 9999999999;
    top: 0;
}
div#mcTooltip {
    float: left;
    border-style: solid;
    position: relative;
    overflow: hidden;
}
div.mcTooltipInner {
    float: left;
    position: relative;
    width: auto;
    height: auto;
}
div#mcTooltip, div#mcTooltip div {
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
}
div#mcttCo {
    position: absolute;
    text-align: left;
}
div#mcttCo em, div#mcttCo b {
    display: block;
    width: 0;
    height: 0;
    overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://www.menucool.com/Content/widgets?v=pWIjgyLaRd3JgPu8kRMWTBEFhPIETaPsvP81TXLgnbE1"></script>
<div class="hide">
  <div id="toolTipContent">Tooltip text goes here</div>
</div>
<br/>
<br/>
<br/>
<object id="object" type="image/svg+xml" data="http://www.schepers.cc/svg/blendups/smiley.svg"><svg width="100" height="100">
  <rect width="100" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" onmouseover="tooltip.pop(this, '#toolTipContent', {position:2})" />
</svg><svg width="100" height="100">
  <rect width="100" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" onmouseover="tooltip.pop(this, '#toolTipContent', {position:2})" />
</svg><svg width="100" height="100">
  <rect width="100" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" onmouseover="tooltip.pop(this, '#toolTipContent', {position:2})" />
</svg></object>

Answer №2

Upon further examination, it appears that the issue with your initial method lies in the fact that the object tag does not accommodate 'onmouseover' https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object. Instead, I have opted to utilize an img tag as a replacement. This adjustment is relatively minor compared to the first solution provided.

var svg = document.getElementById('kiwi');
svg.onmouseover = function() {tooltip.pop(this, '#tooltip');}
.hide {display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://www.menucool.com/Content/widgets?v=pWIjgyLaRd3JgPu8kRMWTBEFhPIETaPsvP81TXLgnbE1"></script>
<div class="">
  <img id="kiwi" height="200" width="200" src="http://s.cdpn.io/3/kiwi.svg">
</div>
<div class="hide">
<span id="tooltip" >
    This is a tooltip.
</span>
</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

The anchorEl state in Material UI Popper is having trouble updating

I am currently facing an issue with the Material UI popper as the anchorEl state remains stuck at null. Although Material UI provides an example using a functional component, I am working with a class-based component where the logic is quite similar. I w ...

Activate anchor classes in several anchor link menus simultaneously

I have a one-page website with multiple menus filled with anchor links. Here is an example of what they look like: <ul> <li><a href="#home" class="active" onclick="closeNav();">Home</a></li> <li><a href="#abo ...

Image sliders are not compatible with ajax content loaders as they do not function properly together

My image slider works fine on its own, but when combined with my ajax content loader, it stops working. Additionally, the image slider only functions properly after refreshing the page on my website. Can anyone suggest a solution to make them work togeth ...

avoiding less than or greater than symbols in JavaScript

I'm encountering an issue while attempting to escape certain code. Essentially, I need to escape "<" and ">" but have them display as "<" and "> in my #output div. At the moment, they show up as "&lt;" and "&gt;" on the page. This ...

The PHP equivalent of converting data to a JSON string, similar to the

When working with PHP's json_encode($array), I've noticed that diacritics can sometimes get messed up. However, if I update my database column to type text and pass javascript-created JSON over HTTP, everything appears fine. The issue arises when ...

Ways to make logo image go over/out of header

I am attempting to create a design where the logo image extends beyond the header and into the content as you scroll, giving the impression that the logo is oversized for the header and protruding from the bottom. Visit the website: For an example of a s ...

Stop the link action following a delay after the mouse is clicked

I'm currently troubleshooting a function that should prevent users from being redirected when clicking and holding onto a link for more than one second. However, the function I implemented is not functioning as expected. I have spent some time reviewi ...

Traversing JSON Data using Vanilla JavaScript to dynamically fill a specified amount of articles in an HTML page

Here is the code along with my explanation and questions: I'm utilizing myjson.com to create 12 'results'. These results represent 12 clients, each with different sets of data. For instance, Client 1: First Name - James, Address - 1234 Ma ...

Utilizing a JavaScript class method through the onchange attribute triggering

I am facing an issue with a simple class that I have created. I can instantiate it and call the init function without any problems, but I am unable to call another method from the onchange attribute. Below is the code for the class: var ScheduleViewer = ...

Send Summernote code data using Ajax to PHP which will then store it in the database

I have implemented a Summernote editor on a page where users can input content. When a user submits the page, I use jQuery to retrieve the HTML code from the editor and then send it to a PHP script for insertion into a database. The HTML retrieved before s ...

What is causing the warnings for a functional TypeScript multidimensional array?

I have an array of individuals stored in a nested associative array structure. Each individual is assigned to a specific location, and each location is associated with a particular timezone. Here is how I have defined my variables: interface AssociativeArr ...

Position the button at the bottom of the card using Bootstrap

While exploring a Bootstrap example utilizing the card-deck and card classes (Pricing example), I pondered over how to align buttons correctly when one list has fewer items than others. https://i.sstatic.net/IGN7K.png I aim to vertically align all button ...

Obtain the final output of a specific class utilizing jQuery

My HTML table structure is as follows: <tbody id="letter-list"> <?php $index = 0; foreach($letters as $letter) { $index++; ?> <tr id="letter-<?php echo $index; ?>"> <td><?php echo $l ...

Generate a series of buttons with generic identifiers that can be easily targeted using getElementById. The IDs will be dynamically assigned

I am looking to dynamically generate unique IDs for a list of buttons, allowing me to easily target specific buttons using getElementById. Here is an example code snippet where each button has the same ID: @foreach (var cat in Model) { <div clas ...

Having difficulty controlling the DOM using cheerio in a Node.js environment

I am currently working on a basic program using Node for the server side with cheerio. Here are the code snippets provided: Server Side: /** * Module dependencies. */ var express = require('express') , routes = require('./routes') ...

Using plain JavaScript (without any additional libraries like jQuery), you can eliminate a class from an element

I'm attempting to locate an element by its class name, and then remove the class from it. My goal is to achieve this using pure JavaScript, without relying on jQuery. Here is my current approach: <script> var changeController = function() { ...

Enhance Your Website with HTML and JavaScript

Here is the code snippet I am working with: sTAT **javascript** var acc = document.getElementsByClassName("item-wrapper"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function(){ this.classList.toggle("selected"); this.nextElementS ...

Using a global variable in Vue and noticing that it does not update computed variables?

Currently, I'm in the process of developing a web application and have begun implementing authentication using firebase. Successfully setting up the login interface, my next step is to propagate this data throughout the entire app. Not utilizing Vuex ...

The yarn installation process is not utilizing the latest available version

Working with a custom React component library my-ui hosted on a personal GitLab instance. In the package.json, I include the library like this: "my-ui": "git+ssh://<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6 ...

Facebook has broadened the scope of permissions for canvas applications

I am in the process of developing a Facebook canvas application that requires extended permissions for managing images (creating galleries and uploading images) as well as posting to a user's news feed. I am currently facing challenges with obtaining ...