Alter the HTML tag once the text is encompassed within it

Can you configure your HTML code to display (B) when the text in (A) is wrapped? If it's not wrapped, then just display (A)

(A)

    <div>
     <span>abcdefgabcdefgabcdefg</span>
     <span>abcdefgabcdefgabcdefg</span>
    </div>

(B)

    <div>
     <img src="a.jpeg"/>
    </div>

Answer №1

With the help of jQuery, you can determine the difference between the innerWidth and scrollWidth of a specific element by ensuring that your CSS includes white-space: nowrap.

If the scroll width exceeds the inner width of the element, it indicates that the content within will wrap, allowing you to showcase alternate content instead.

For instance:

$(document).ready(function() {
  if ($("#example")[0].scrollWidth > $("#example").innerWidth()) {
    // The text will wrap, so display alternative content
    console.log("Text will wrap");
  }
});
#example {
  white-space: nowrap;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="example"> <span>abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde</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 error message regarding pdf.worker.min is stating that the use of 'import' and 'export' is limited to within module code and cannot be used outside of it

Upon attempting to deploy a build to Vercel, I encountered the following error message: Failed to compile. static/media/pdf.worker.min.50acc843.mjs from Terser x 'import', and 'export' cannot be used outside of module code ,-[18:1 ...

Using JavaScript to transform JSON information into Excel format

I have tried various solutions to my problem, but none seem to fit my specific requirement. Let me walk you through what I have attempted. function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) { //If JSONData is not an object then JSON.parse will ...

Troubles with Flex wrap in CSS3 on iOS 10.3.2 browsers (Chrome and Safari)

Before sending me to other similar questions, keep in mind that I have already gone through them, attempted the solutions, and they are not resolving my issue. The problem I am encountering involves the flexbox layout. I am using the flexbox layout for ce ...

What is the best way to show only the weekdays on the x-axis?

My goal is to create a scatter-linked graph using D3.js, showcasing the people count for various shifts on different dates in January 2020. Here is the code snippet I am working with: <!DOCTYPE html> <html lang="en" > <head> & ...

Encountered an issue with updating the list: unable to read property 'map' of undefined

I receive a list of categories from the server, and each category has an 'Edit' button next to it. Clicking on this button opens a modal with an input field and a submit button. When I edit a category and click the button, my list should be updat ...

What is the reason javascript struggles to locate and replace strings with spaces in a URL?

Let me begin by sharing the code I'm currently working on so that you can easily follow my explanations. Apologies for the French language used, as this website is being developed for a French-speaking school. I have eliminated irrelevant sections fro ...

Clicking the identical button on multiple overlapping elements using Selenium and Node.js

Working with Selenium has presented a challenge when trying to close multiple HTML elements that share the same "close" button and overlap each other. The initial approach of looping through them individually to close seemed unsuccessful. The first step i ...

code malfunctioning following conversion from jQuery to pure JavaScript

I have successfully converted the following script from a jQuery-based Ajax to Pure JavaScript-based Ajax, but I am encountering issues with its functionality. Here is the original jQuery-based script: var cart = { 'add': function(product_id, q ...

The template literal expression is being flagged as an "Invalid type" because it includes both string and undefined values, despite my cautious use of

I am facing an issue with a simple component that loops out buttons. During the TypeScript build, I encountered an error when calling this loop: 17:60 Error: Invalid type "string | undefined" of template literal expression. In my JSX return, I ...

Encountered an error while trying to load a resource: the server returned a 404 (Not Found) status code while attempting to edit a form using

I am facing an issue with navigating to the correct path after editing a form in React. Following the update, the web page refreshes and unexpectedly logs me out of the site even though there are no errors detected during the process. The console displays ...

What causes the Top Navbar in Twitter Bootstrap to exceed the container when fixed?

While working with the default fixed-top navbar in Bootstrap, I encountered an issue where it spans the full width of the viewport. In order to contain it within a specific container div, I tried applying a CSS override for the left side: .navbar-fixed-to ...

Using async/await with recursion in NodeJS for handling express requests

My code contains a function named GetAllData() that triggers the GetPurchaseData function, which in turn calls itself recursively until all the data is loaded. async function GetAllData(){ console.log("starting loading purchase data"); await G ...

Adding a div element to a React component with the help of React hooks

I'm currently diving into the world of React and experimenting with creating a todo app to enhance my understanding of React concepts. Here's the scenario I'm trying to implement: The user triggers an event by clicking a button A prompt app ...

How can you disable form functionality in Laravel?

Currently, I am in the process of creating a project that will include a demo for users to test. However, I want to ensure that they do not have access to the admin area. My initial solution involved using JavaScript to prevent any forms with post methods ...

Element Style Does Not Align with Computed Z-Index

Currently, I am in the process of developing an HTML5 video player. However, I have encountered a problem where my custom controls become unclickable once the video element is set to fullscreen mode. This issue arises because the video's z-index is se ...

Animated smooth updates in d3 line graphs are the key to creating dynamic and

I'm attempting to modify an example of Animated Line Graphs from: http://bl.ocks.org/benjchristensen/1148374 <div id="graph1" class="aGraph" style="width:600px; height:60px;"></div> <script> function draw(id, width, height, upd ...

How to Track URL Modifications within an Iframe on an Angular Ionic Mobile Application

Issue I'm currently working on identifying modifications in the URL of an embedded <iframe /> within an Ionic (v5) app using Angular. Solution // ts file @ViewChild('myIframe') public iFrame; backButtonSubscription: Subscription; ...

Executing a JavaScript function within a PHP echo statement

I am completely new to working with AJAX and PHP echo variables or arrays. However, I have managed to successfully display images on my website by passing variables from AJAX to PHP and back. The only problem I am encountering is when I try to call a Javas ...

Are there any CSS selectors similar to :empty that can match elements with children that have display:none?

Looking for a solution to the issue stated in the title, this code snippet is causing some trouble: .group { width: 100px; height: 100px; background-color: blue; } .group:empty { background-color: red; } .hideme { display: none; } <div cl ...

What is the best way to extract webpage data in an android application?

Is there a way to automate entry of a string into an input tag on a website form, click a button, and retrieve the link generated on another page in the background using JavaScript or some other method? Specifically, I am looking to achieve this on the w ...