Identify text truncation using JavaScript

I have a container with CSS that truncates text to show ellipsis when it reaches a certain width. Despite the truncation, the full text remains unchanged inside. To test this feature, I'd like to be able to access the truncated text with JavaScript. Is there a way to retrieve the truncated text marked with ellipsis using JavaScript?

@media (max-width: 1129px) {
    #mydiv {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100px;
        display: inline-block;
    }
}

Answer №1

Doing this task isn't simple, the only way is to manually use JavaScript by:

  1. Finding out the visible width of the div
  2. Edit your text to eliminate any characters that extend beyond this width

This issue has also been addressed in this discussion Ellipsis and text trimming techniques

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

Incorporate PNG files with pre-defined labels in a React element

In my application, there is a collection of PNG images with filenames consisting of only 2 letters like aa.png, ab.png, ac.png, and so on. Additionally, there is an API endpoint that retrieves an array of objects with a property "name" containing 3 letter ...

Looking for advice on successfully implementing createMultiMaterialObject in THREE.js version r62?

I am having trouble getting createMultiMaterialObject to work as expected. My goal is to have a wireframe material displayed on top of a solid material. However, the multimaterial function only seems to display the first material in the array. Here is the ...

What is the best way to create a clickable entity that is being dynamically generated in a networked A-Frame environment?

I'm attempting to use a networked A-frame to create a new entity upon page load. I want to incorporate various functionalities, such as hover and click events, on these entities. However, my attempts to make them clickable have been unsuccessful. f ...

JavaScript date parsing problem

When I fetch values from a datatable that has a column of type DateTime, I then serialize it using the Json.Net library. jsonData = JsonConvert.SerializeObject(datatable); The original date in the datatable was: 2013-04-03 04:01:24.623. After serializati ...

The challenge with the mousewheel function in THREE.js Editor

Attempting to create a basic scene in the THREE.js Editor. Using the built-in Script editor, all control functions seem to be functioning correctly except for the mousewheel (I've tried mousedown, mousemove, etc.). I even attempted to add a listener ...

Effective strategies for efficiently managing a multitude of key codes within an event handler?

This might lean towards a pure JS question, but I'm including Angular2 as well in case any developers with Angular expertise have alternative techniques for this While developing an angular2 application, I'm implementing a keydown event handler ...

Discovering elements in Selenium that are dynamically loaded using JavaScript

My endeavor to automate form completion using selenium is being hindered by a peculiar issue. The particular form I am interacting with does not load instantly in the HTML code; instead, it is loaded dynamically through JavaScript after the initial page lo ...

Tips for aligning a dropdown button with the other elements in your navbar

I followed the code outline from a tutorial on We3schools, but I'm having an issue with the button not aligning correctly with the navbar. https://i.sstatic.net/fSRIT.png I attempted to adjust the code for proper alignment before publishing, but was ...

Trigger javascript function after clicking on two buttons

I am currently working on developing a web game inspired by Duolingo. In a specific scenario, I need a function to be triggered after two button clicks to progress further. However, I am facing challenges in finding solutions online or understanding it on ...

A lone slant positioned at the lower border of an object

Currently, I am working with two images stacked vertically. The dimensions of the top image are set at 100% width and 350px height, while the bottom image size is not a major concern. I am specifically interested in skewing the top image and maintaining a ...

Transform the array by utilizing its own elements

I came up with a solution to a problem I was facing, but I'm not entirely comfortable with it. It feels risky to me, like using an array to redefine itself is not a good coding practice. It's similar to trying to explain a word by using the word ...

Is it possible to display an HTML table column-by-column rather than row-by-row?

I have a scenario similar to this: https://jsfiddle.net/ahvonenj/xrrqzypL/ The number of objects in the data-array could be either even or odd, making all these examples valid: var data = [ { title: 'Title A', content: &apo ...

Tips for hiding a sidebar by clicking away from it in JavaScript

My angular application for small devices has a working sidebar toggling feature, but I want the sidebar to close or hide when clicking anywhere on the page (i.e body). .component.html <nav class="sidebar sidebar-offcanvas active" id="sid ...

Adding a navigation bar to every administrator page while excluding it from shop pages can be achieved by creating a

I am facing a challenge in implementing a navbar and sidebar on all admin pages, except for the shop page. The issue arises because I have set my sidebar and navbar to be global. My goal is to make them global only for admin pages and not for the shop. He ...

Utilize the power of jQuery accordion to organize and display your table

Is there a way to integrate jQuery UI Accordion with an HTML table so that columns can be collapsible? I have tried various methods but haven't been successful. Currently, this is what I have implemented: $(".col1, .col2").addClass("hidden"); $(".s ...

Transferring information to PHP script using only JavaScript (Ajax)

I have a good understanding of jQuery: $.ajax({ method: 'POST', url: 'send-data-to.php', data: {data:"data"} }) However, I am unsure of how to send data that is accessible via $_POST using pure JavaScript. The reason for my inqu ...

Encountering the error message "Unable to read properties of undefined when attempting to set an attribute for the input element on a webpage."

Trying to assign a value attribute to an input tag web element, but encountering the following error: Here is the code I have used: 'document.getElementsByName('date')[0].setAttribute('value','2022-11-29');' Howeve ...

Evolution of the material through a fresh new slide

Can someone assist me with an animation issue? I have a slideshow consisting of 4 images that are supposed to transition automatically after a set time interval. Upon initially loading the webpage, the animation works perfectly as intended. However, subs ...

Is there a way to invoke a function from a component that is neither a direct child nor parent component?

Module X let searchQuery = .... retrieveData(searchQuery); Module Y //implementing the use of Module X's parameter ...

Performing a JavaScript Axios POST request following a series of iterations using a while loop with

Just getting started with async/await and feeling a bit lost. I'm trying to figure out how to send an axios post request after a while loop finishes. Is there a way to wrap the while loop in an async function and await for it? Here's the code s ...