What is the best way to set the title of the <span>/<div> tag in order for it to display the <br/> tag in the content and show a tooltip on separate lines?

I have this AngularJS code sample that reads the HTML tag within the data and displays the content on the screen with the following message:
Hi Mike!
Good Morning!!!

I need to modify it so that the title or tooltip also shows the content in two separate lines, considering the br tag within the data. Currently, the title/tooltip displays as Hi Mike! <br/> Good Morning!!! in a single line.

Here is the Sample Code:

<html ng-app>
<head>
    <title>Sample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.1.1/angular.js"

type="text/javascript">

<script type="text/javascript">
            function SampleCtrl($scope) {
                $scope.message = "Hi Mike! <br/> Good Morning!!!"
            }   

        </script>
</head>
<body>
    <div ng-controller="SampleCtrl">
        <span title="{{message}}" ng-bind-html-unsafe="message"></span>
    </div>
</body>
</html>

Answer №1

Instead of relying on the HTML break tag, you can use a new line character \n. The title attribute treats its content as plain text, so it won't interpret any HTML tags.

Take a look at this example: http://jsfiddle.net/tcjbkqqz/

var app = angular.module('test', []);
app.controller('SampleCtrl', function($scope) {
     $scope.message = "Hello John!\nHave a great day!!!"
});

Answer №2

Take a look at $sanitize - https://docs.angularjs.org/api/ngSanitize/service/$sanitize

You have the option to utilize it within a directive to properly display HTML in model content, rather than just displaying it as plain text.

<span ng-bind-html="message"></span>

In a recent project where user input needed to be trusted, I developed a filter using $sce. https://docs.angularjs.org/api/ng/service/$sce

.filter('trusthtml', function($sce) {
    return function(val) {
        return $sce.trustAsHtml(val);
    };
}) 

For your specific case, you could implement it like this -

<span ng-bind-html="message | trusthtml"></span>

Which version of angular are you currently using? It appears that ng-bind-html-unsafe (the directive being used) has been deprecated, and relevant documentation is no longer available.

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 Mongoose _id seems to be malfunctioning when a custom value is used instead of the default value

Greetings! I am currently working with Mongoose and have a collection named 'tag' with the following schema: const tagSchema = new Schema<ITag, ITagModel>( { _id: { type: String, unique: true, required: true, }, ...

Is there a way to preserve the original color format without converting it to RGB

When applying a hsl color in JavaScript, it ends up being converted to RGB instead of staying as an HSL color. document.body.style.backgroundColor = "hsl(0,100%,50%)" document.body.style.backgroundColor; // "rgb(255, 0, 0)" I wanted to set an HSL color a ...

Error Handling in React with Stripe Payments

I've been attempting to integrate Stripe into my project, but I keep encountering the following error: Error: Cannot read property 'setPublishableKey' of undefined Below is the code snippet I am using: import React from 'react&apos ...

Effortless method to handle package.json configurations

Is there a better approach for seamlessly transitioning between using npm link and git, or another solution that caters well to both front end and back end developers? The dilemma I'm facing revolves around developing a website that utilizes multiple ...

enhancing the appearance of the initial sentence in the closing passage through CSS styling

Is there a way to make only the first sentence in the final paragraph bold using CSS? Specifically, I would like to add extra padding to the top of this last paragraph so that it wraps around an image. However, when I increase the bottom padding of the flo ...

Most efficient method to retrieve a specific element from an array without the need for iteration

Within one of the controllers in my Angular application, I have set a variable like this: SomeService.get({}, function(data){ // This assigns xyz as the data list retrieved from the // resource within the controller's scope $scope.xyz = ...

What is the method by which frameworks such as Angular encapsulate CSS within a component?

I have a question out of curiosity, rather than seeking a solution to a specific issue. Can you explain how frameworks such as Angular manage to keep CSS contained within a component and prevent it from spreading across the entire webpage? ...

Choose the tr element using JQuery when none of the images in the td contain the file name example.png

Is there a way to select the tr elements that do not contain example.png in any of their td elements? Each image has an anchor, so I was thinking of selecting based on that... $(document).ready(function(){ $("td > a").parent(). ...

Implementing dynamic content loading using CSS and jQuery upon link/button activation

I'm facing an issue where I am attempting to align content to the left side of my screen to display a feed while also ensuring that the pushed content is responsive. Unfortunately, the feed is not visible and the content remains unresponsive. Is ther ...

Steps for building HTML based on the provided layout

I am looking to dynamically create the following HTML structure: <tr> <td><label class="mt-checkbox mt-checkbox-outline"><input type="checkbox" class="chkclass">A<span></span></label></td> <td&g ...

PHP was unable to retrieve the values sent by AJAX

Hey there! I'm currently facing an issue with passing values from a link and using $_GET to retrieve those values. When testing it on my local environment, everything works fine. However, once I moved it to a live site, the parameters' values are ...

Having issues with the functionality of the Jquery-turbolinks gem

Utilizing the jquery-turbolinks gem allows me to ensure that $(document).ready() is triggered with each page load, even when rails turbolinks is enabled. I carefully followed the instructions on the github page for installing the gem, but it appears that ...

I am currently working with a for loop within an object in JavaScript, but there seems to be a problem with one

Here is a function called validator: import validator from "validator"; import isEmpty from "is-empty"; export default function validate(data) { const errors = {}; for (const property in data) { console.log(property); //< ...

Unable to get jquery toggle() to function in chrome or safari

Having trouble with the toggle() function in Chrome and Safari. I've tried using noconflict() but it hasn't worked. In Chrome and Safari, the first recipe translation appears below the German one. The other 2 recipes are working as expected. How ...

Use jQuery UI draggable to create a clone with a shadow effect

Attempting to replicate this: This is what I have for the draggable element: $( function() { $('#sidebar .question-constructor').draggable({ connectToSortable:'#preview', scroll: false, helper : 'clone ...

Is it feasible to separate the bin and lib dependencies in an npm module?

Imagine you have already successfully released a nodejs module on npm. This module is straightforward - just install and import it, provide a string and a config object, and it will return a string for you. Now, here's the twist: you want this module ...

Changing the properties of the admin bar when it is hovered over in

After successfully styling a button on the admin bar, I noticed a few imperfections in the implementation. The button in question is situated on the admin bar and is labeled "maintenance". Upon clicking the button, jQuery triggers the addition of the clas ...

Exploring CSS3 animations: customizing animations for individual elements based on scroll movements

Can you help me with CSS3 animations triggered by scrolling? I came across a code that reveals a DIV element as the user scrolls down: <script type="text/javascript"> $(document).ready(function() { /* Every time the window ...

Interactive chat feature with live updates utilizing jQuery's $.Ajax feature for desktop users

I came across a script for real-time chat using $.ajax jQuery, but it only refreshes my messages. Here's an example scenario: I type: Hello to You, and I see this message refreshed. You reply: Hey, in order to see your message, I have to manually refr ...

Iterate through TD elements to retrieve their values

Is there a way to retrieve the value of my TD for deleting dates from my database using jQuery? This is the current code snippet I have: $(document).ready(function() { $('table#delTable td a.delete').click(function() ...