Tips for wrapping text to fit the width of a column

Hello, I have a table with varying column widths as shown below

${"#goldBarList"} table tr td:first-child{width:5%;}
${"#goldBarList"} table tr td:first-child+td{width:5%;}
${"#goldBarList"} table tr td:first-child+td+td{width:6%;}
${"#goldBarList"} table tr td:first-child+td+td+td{width:7%;}
${"#goldBarList"} table tr td:first-child+td+td+td+td{width:7%;}
${"#goldBarList"} table tr td:first-child+td+td+td+td+td+td+td{width:11%;}
${"#goldBarList"} table tr td:first-child+td+td+td+td+td+td+td+td{width:11%;}
${"#goldBarList"} table tr td:first-child+td+td+td+td+td+td+td+td+td{width:11%;}
${"#goldBarList"} table tr td:first-child+td+td+td+td+td+td+td+td+td+td{width:11%;}
${"#goldBarList"} table tr:first-child{read-only:true; font-size:90%; font-weight:bold;}

I utilized the following CSS snippet:

${"#goldBarList"} table{
table-layout:fixed;
word-wrap:break-word;
}

to fix the table and wrap text in cells. However, the issue is that it does not wrap the text according to the column width. Even for 5% or 11% width columns, it wraps text wider than 8 characters. How can I achieve text wrapping based on column width?

Answer №1

To implement text wrapping in javascript, you can use the following function:

var wrapText = function(str, length){
    if(str == null || str == '') return '';
    return (str.length > length) ? (str.substr(0,length) + "...") : str;
}

You can call the function like this:

var name = "This is a very long text that needs to be wrapped";
$(selector).attr('title', name).html(wrapText(name, 10));

The title attribute of the element will display the default tooltip.

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

Issue with Google Charts - Chart is unable to render because data table has not been defined

Using Ajax, I attempted to set an Interval for my Google Chart, but unfortunately, the Chart is not being drawn. Instead, I repeatedly receive the error message: "Data table is not defined." every 5 seconds. If you want to see a screenshot of the error mes ...

Create a loop in JavaScript to iterate through elements using both a while loop and the

I have a JSON object (returned by the server) and I am looking to iterate through it with either a while loop or forEach method in order to display the products in a shopping cart. The name of my object is response2, which contains the cart products. I w ...

Is HTML5 Device Orientation the answer to a dependable compass system?

I am currently developing a project for mobile web that requires access to the compass direction of the user's device. At the moment, my code is quite basic, but here is what I have: var updateDirection = function (evt) { $("#dire ...

What is the syntax for implementing conditional statements within the style jsx of Next.js?

Recently, I've been exploring the use of <style jsx> in my Next.js project and encountered a scenario where I needed to style an element based on a conditional statement. Additionally, my project relies on tailwindCSS for styling: <div classN ...

Mac users may experience lag when using Javascript parallax effects

I created a parallax page where the background image changes using JavaScript translateY(- ... px)' similar to what you see on the firewatch website. On Windows, the parallax effect works smoothly. However, on macOS it is smooth only in Safari. All ...

Using Nuxt and Cloudinary to seamlessly upload images from the client side directly to the Cloudinary platform

Is there a way to directly upload images from my Nuxt (vue) app to Cloudinary without involving a server? I've been searching for information on how to accomplish this but haven't found any concrete solutions. <v-file-input v-else ...

Utilizing jQuery ajax to make a request to a node.js server

This question may seem simple, but I am struggling to figure out the problem in my code. I am trying to send data to a Node.js API using a jQuery AJAX call. Here is my Node.js API snippet: .post('/createPerson', global.bodyParserJson, function ...

What is the most efficient way to handle dependencies and instantiate objects just once in JavaScript?

I am interested in discovering reliable and tested design patterns in Javascript that ensure the loading of dependencies only once, as well as instantiating an object only once within the DOM. Specifically, I have the following scenario: // Block A in th ...

Is it possible to link multiple references to a single Element/Node?

I am working on a function component that needs to pass the incoming ref from the parent to a div it is rendering. Additionally, I want to create and assign a separate ref inside the component to the same div. However, due to an element only accepting one ...

Incorrect Results from Angular Code Coverage

Currently using Angular.js, Karma, Karma-coverage (Istanbul), and Jasmine for my stack. While conducting Code Coverage analysis on my app, I encountered an issue which leads to my question - Service A is showing up as covered by tests (in green) even thou ...

Having trouble choosing elements with angular.element within ng-repeat loop

In my HTML code, I am using an ngRepeat element: <ol id="animationFrame"> <li ng-repeat="animationImage in animationImages" ng-repeat-listener> <img ng-src="{{animationImage.src}}" id="{{animationImage.id}}"> </li> </ol& ...

Safari's problem with CSS transitions

This issue is specific to Safari, as it has been tested and works fine in Chrome, Opera, and Firefox. Upon hovering over a certain div (1), a child div (2) slides out by adjusting the left property. However, the child elements (buttons) within the second ...

What could be causing the undefined value of $routeParams?

I've encountered an issue while attempting to utilize a service for managing an http request: angular .module('app') .factory('stats', function($http){ return { getStats: function(path) { ...

Passing an array of items as a property to a child component in React with Typescript is not possible

In my project, I have multiple classes designed with create-react-app. I am trying to send an array of objects to child components as illustrated below. Items.tsx import * as React from 'react'; import ItemTable from './ItemTable'; imp ...

When submitting an Asp net mvc Form, some fields may not be posted as expected

I'm facing an issue with my ASP.NET MVC application form. Some fields that should be posted are missing, and I can't figure out why. <form id="FormRegistroUsuario" action="/Account/AdminSecurityUserAccountAdd"> <fieldset> ...

Looking for assistance in transferring information from one webpage to another dynamic webpage

I'm in the process of building a website to feature products using NextJs. My goal is to transfer data from one page to another dynamic page. The data I am working with consists of a json array of objects stored in a data folder within the project. Wh ...

Strategies for delaying the loading of CSS when importing

import 'react-dates/lib/css/_datepicker.css' The CSS mentioned can be deferred since it is not critical. Is it possible to defer the loading of CSS when utilizing import? I found information on deferring CSS loading using <link> from Goo ...

directive does not execute when the <Enter> key is pressed

I recently came across a helpful post on Stack Overflow about creating an Enter keypress directive. After following the instructions, here is my JavaScript code that replicates the functionality: JavaScript var app = angular.module('myApp', [] ...

Error: 'error' is undefined

Error Alert: The code is encountering a ReferenceError, indicating that 'error' is not defined in the following snippet: app.post('/register', function(req, res) { var hash = bcrypt.hashSync(req.body.password, bcrypt.genSaltSync(10)) ...

What is the best method for showcasing various content using a uniform accordion style in React?

What is the most efficient way to display various content within multiple accordions? view image description here This is the current approach I am taking in my project, where shipping information and delivery options will involve different textboxes, labe ...