Issue with jQuery tooltip not showing information when there is a space present

I am attempting to display data on a mouse hover event using a jQuery tooltip. The data I have looks like this:

{"description":"marry christmas","date":"2016-12-25
}" which I received from the server as a JSON string. I am parsing it in my calendar as follows: holi is the variable holding the JSON string above this is my import

<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>



   $.each(JSON.parse(holi), function(i, item) {
        var holidayDate=item.date+"";
       var parts= holidayDate.split("-");
       alert("parts:"+parts[2]);
       document.getElementById(parts[2]+parts[1]).style.color="green";
      var va= document.getElementById(parts[2]+parts[1]).innerHTML;
       document.getElementById(parts[2]+parts[1]).innerHTML="<label id="+parts[2]+parts[1]+" title="+
       item.description+">"+va+"</label>";
       $("#"+parts[2]+parts[1]).tooltip();

    });
    }

Now when I hover over 25th December, it only shows "marry" instead of "merry Christmas." I tested this in Chrome. Can someone please help me identify what's wrong with this setup?

Answer №1

Make sure to enclose the title attribute value in quotes. Here's a suggestion:

document.getElementById(parts[2]+parts[1]).innerHTML
="<label id="+parts[2]+parts[1]+" title='"+item.description+"'>"+va+"</label>";

Take note of the single quotes surrounding item.description after the = and before the >.

Furthermore, it's advisable to refrain from generating DOM elements directly in JavaScript as it can lead to errors and maintenance challenges.

Answer №2

When creating the title in HTML, it is important to consider how the HTML will be interpreted by the parser. If your title contains spaces, you must enclose the HTML attribute value in quotes.

You can improve your code by utilizing jQuery APIs since you are already using the library:

$.each(JSON.parse(holi), function(i, item) {
  var holidayDate = item.date + "";
  var parts = holidayDate.split("-");
  var dayId = parts[2] + parts[1], day = $("#" + dayId);

  day.css("color", "green")
      .html($("<label/>", {
        title: item.description,
        html: day.html()
      }))
      .find("label").tooltip();

});

With jQuery, you can create new HTML elements without having to deal with messy quoting by using the following format:

$("<tagname/>" {
  attribute: value,
  attribute: value,
  // ...
})

In this example, the code sets the "title" attribute first and then adds content; the "html" attribute functions similar to the jQuery .html() method.

Lastly, it selects the newly created <label> element and applies the .tooltip() method to it.

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

My navigation menu has a nested ul, but on mobile devices, it doesn't display all the items in the list. What could be causing

When I click on "Products" in my main navigation, only the first 6 items are displayed from a nested ul. How can I make all of them display when the user clicks on "Products"? Is this a CSS issue or a problem with the script? Here's a screenshot for r ...

What methods can be used to identify the specific Router component being rendered in React?

I am currently working with a Navbar component that I want to render with different CSS styles using styled-components based on the route of the component being rendered. How can I determine whether that component is being rendered or not? const NavbarCont ...

Testing the MatDialog Component

Currently, I am in the process of creating a unit test for my confirmation modal that relies on MatDialog. The initial test I have set up is a simple one to ensure that the component is successfully created. Below is the code snippet from my spec file: im ...

Generating a File that Imports Components and Instantly Exports Them Once More

Having trouble with this code. Initially, I had: // file 1 import Box from '@/components/Box' import Popup from '@/components/Popup' const MDXComponents = { Box, Popup } // using MDXComponents somewhere in file 1 Now, to manage t ...

How to add dimensions to a background image using CSS3

I would like the background image to have a size of 636px by 1140px instead of applying it to the div element directly. This is because I want to avoid scrollbars due to the height of the div. When I specify the width and height for the parent div, the ba ...

Is it possible to manipulate the carousel image within the div element?

I am working on a Bootstrap code snippet that showcases an image using the w-100 class to span the full width of the page. However, the challenge I'm facing is making sure the image is visible to users while keeping it small enough so they won't ...

`Unable to properly transmit parameters using the "application/json" format`

I spent the entire morning trying to come up with a solution (browsed through every related post on SO and conducted several experiments myself), but unfortunately, I was unsuccessful. Here's the Server code: Controller: [HttpGet] public JsonRe ...

Implementing a feature in React where the active class is applied only to the next element upon clicking

I'm just starting out with React and working on a custom menu bar project. The Mobile menu includes 2 dropdown submenus, which I want to toggle open and close by clicking an arrow. Currently, the arrows are functioning, but when I click on one arrow, ...

Having difficulty locating the xpath list, attempting to utilize a wildcard to search for text or style

My task is to locate the XPath for the section under “Main Lists” on this specific site. This is what I have so far: //div[starts-with(@class, ('sm-CouponLink_Label'))] However, this search yields 32 results... `//div[starts-with(@class, ( ...

Is there a way to handle button click event when utilizing this.props.children in React?

I have recently completed a React component that serves as a modal wrapper. The content for this modal is passed through the props.children property. Inside the content, there is a button that, when clicked, should trigger an event in the parent component. ...

Deserializing Nested JSON Objects in Delphi 2012

I am currently developing a wrapper for Dropbox in Delphi 2012. One issue I am facing is the deserialization of JSON responses. When requesting a list of folders and files from my account, the response looks like this: { "hash": "some_hash", "thum ...

Expanding interfaces dynamically in Typescript

Currently, I am facing a challenge while attempting to integrate an existing React Native module equipped with the following props: useComponent1: boolean useComponent2: boolean This is how the implementation looks like: render(){ if(useComponent1){ ...

How to Pass Parameters to ASP.NET MVC Controller using AJAX

Can anyone help me with this issue? I keep getting the error message stating: "There is no argument given that corresponds to the required formal parameter 'SSN' of HomeController.GetCICO(string)" This error seems to be originating from the f ...

Verify email availability using Ajax in registration form

I have implemented this script to verify if an email is registered in the database using ajax. however, I am facing an issue where it only checks once - if a user enters a registered email and then inserts another email, the variable in checkemail.php does ...

Are JS commands enough for using Node.js dom APIs like jsdom and cheerio, or do I have to rely on jQuery for these tasks?

Is there a DOM API available for Node.js that allows me to use pure JavaScript commands? I prefer not to use jQuery as I already have existing code in vanilla JS. ...

Error occurred due to incorrect JSON format for the quoted string

I am encountering an issue with Codable parsing... here is a snippet of my code: class Test: Codable { let resultCount: Int? let quote: String? } var json = """ { "resultCount" : 42, "quote" : "My real quote" } """.data(using: .utf8 ...

Shorten the text before the hyphen using jQuery

I'm interested in learning how to shorten the word that appears before a hyphen in a string. For example: [Jazz] - The 1970's Blues should become... The 1970's Blues While I know how to accomplish this task in PHP using various function ...

Disabling the Bootstrap tooltip feature is a quick and easy process

Is there a way to turn off bootstrap tooltip on my website? I activated it with the code below: function activateTooltip() { const toolTips = document.querySelectorAll('.tooltip'); toolTips.forEach(t => { new bootstrap.Tooltip(t); } ...

What is the best way to switch to a different page in NextJs without causing a full page reload?

In my observation of NextJs, I've noticed that when clicking on a <Link> to navigate to another page, the getInitialProps function is called even if it's the same page being visited. For example, on the /profile page, there are two compone ...

Mobile View Not Displaying Bootstrap Column

I'm currently working on creating a two-column layout, one for text and the other for an image. Everything appears correctly on desktop view, but on mobile devices, the image column is not showing up. How can I adjust it so that on mobile devices, the ...