Design portions of text that are not enclosed in a div element

Currently, I am retrieving data from an API through a SOAP call.

After pulling in the data, the resulting HTML structure appears as follows:

<div class="component_wrapper">
2 man Teams<br>
20 Min AMRAP<br>
50 Double Unders<br>
50 Push Ups<br>
50 Toes 2 Bar<br>
50 Push Press 95/65<br>
50 Heavy Lunges 45/25<br>
</div>

I am looking to style two different parts of this content separately - the numbers and the descriptions. However, wrapping all the numbers in a span tag would also apply styling to elements like "95/65", which is not desired.

My approach involves identifying everything before the first space and wrapping it with

<span class="count"></span>
, while everything after the first space should be enclosed in
<span class="description"></span>
. The space itself should be removed. In cases where there is no space present, the text should be wrapped in
<span class="description"></span>
.

The reasoning behind this logic is that even long numbers like 1000 will be properly styled, while text without numbers will be treated as descriptions.

Is it feasible to implement such functionality? The closest code snippet I could find to achieve this is:

str.substr(0,str.indexOf(' ')); // "2"
str.substr(str.indexOf(' ')+1); // "man Teams"

Thank you.

Answer №1

To extract the numbers from each line and place them in specific spans, I would iterate through the lines with a regex pattern:

var text=element.innerHTML,newtext="";
text=text.split("<br>");
for(var i=0,l=text.length;i<l;i++){
    newtext+=text.replace(/^(\d*)\s(.*)$/,"<span class='count'>$1</span><span class="description">$2</span><br/>");
}
element.innerHTML=newtext;

Updated Code

var text=document.getElementById('wod').innerHTML;
var newtext="";
text=text.split("<br>");
for(var i=0,l=text.length;i<l;i++){
    newtext+=text[i].replace(/(\d*)\s(.*)$/,"<span class='count'>$1</span><span class='description'>$2</span><br/>");
}
document.getElementById('wod').innerHTML=newtext;

Answer №2

Remember that span elements do not possess a predefined height:

'Working in pairs'.replace(/^(\d)+\s(.*)$/, "<span class='number'>$1</span><span class='description'>$2</span>";

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

Implementing access restrictions for modules in NodeJS

In Node, is it possible to limit access or permit access only to specific modules from a particular module? Should I consider replacing the require function and object in the global scope for this purpose? I have concerns about the security of a certain mo ...

Obtaining IDs of Divs that have been Dragged into a Drop Zone upon Clicking a Button using JavaScript

I'm currently working on a solution to extract the ids of dragged divs once they have been placed in the drop zone. Each drag component is assigned an id ranging from drag1 through drag8, while the drop zone is labeled as div drop zone. Since there a ...

AngularJS allows for submitting form data to a new window by utilizing the form

At the moment, I have a JavaScript function that sends a form POST request and opens it in a new window. Now, I want to convert this into AngularJS. Here's the current function. The three parameters passed in determine the post URL and some data valu ...

Chrome Bug with Fixed Position Background

Just finished creating a website that features fixed images as backgrounds with text scrolling on top of them. I've noticed an issue when using Chrome - the scrolling stops briefly between backgrounds, pauses for about a second, and then resumes. I&ap ...

How does Firefox still autocomplete even with a different input label?

How does Firefox decide where to autofill passwords and usernames? I've noticed that even after changing the name, id, title, or class of an input element, Firefox still automatically fills it with my password or email address. ...

With Bootstrap 4 navigation bar, once a container is added, the elements will automatically align to the right

Greetings! I am a newcomer to the world of bootstrap, so please bear with me if my question seems too basic. My current challenge involves creating a navbar using bootstrap. However, I have encountered an issue where the logo remains on the left side whi ...

The functionality of d3.dispatch() is not performing as anticipated

Recently, I delved into the world of D3.js to explore its capabilities. One particular concept that caught my attention is d3.dispatch(), and I've been experimenting with it for about a week now. Below is a simple example: <script src="d3.min. ...

Is there a way to manipulate the DOM without relying on a library like jQuery?

My usual go-to method for manipulating the DOM involves jQuery, like this: var mything = $("#mything"); mything.on("click", function() { mything.addClass("red"); mything.html("I have sinned."); }); Now I am looking to achieve the same result usin ...

React frontend unable to retrieve JSON data from Rails API

I have developed a backend Rails API and confirmed that the endpoint is being accessed by monitoring my server in the terminal. Additionally, I am able to view the response in Postman. However, I am facing an issue where the payload is not returned in my R ...

Add a CSS class to an innerHTML element just a single time

I currently have 2 files available: data.php page.php The data.php file is responsible for fetching a row from a SQL database and sending it to the page.php file. Within the page.php file, there is a JavaScript script that receives this row through AJAX ...

How can I enhance the functionality of AJAX in Symfony 1.4 when using jQuery, specifically in cases where a select box's options depend on the selection of another select box?

In my Symfony 1.4 application, I have three interconnected tables outlined below. Table 1: Conflictos1: connection: doctrine tableName: conflictos_1 columns: id: type: integer(4) fixed: false unsigned: false primary: tru ...

What is the best way to update specific content with fresh URLs?

I am interested in keeping certain sections of my page static, such as the header and footer, while allowing the main content to change dynamically. Additionally, I would like the URL to update based on the new content being displayed. Although I am famil ...

Having trouble establishing a connection to the FTP server through the "ftp" package provided by npm

Attempting to establish a connection to a Secured FTP server using the "ftp" package. When connecting to an unsecured server, everything functions as expected with all events firing and content being displayed. However, upon trying to connect to a server ...

Steps to make a div that perfectly matches the size of the image inside, ensuring responsiveness

Currently, I am in the process of creating a mobile e-mail template without the use of JavaScript. The main requirement is for the template to be responsive. My goal is to insert multiple images in a row, with the ability for the images to scale down when ...

Using the TypeScript compiler API to determine the location in the generated code of a particular AST node

I am aiming to retrieve the specific TypeScript AST node's location (start and end) in the emitted JavaScript file. Consider this code snippet: const program = ts.createProgram(tsconfig.fileNames, tsconfig.options); const aNode = program.getSourceFi ...

How to fix Bootstrap 4 Card Header and Form-Control wrapping onto a new line

I am currently working with Bootstrap 4 and facing an issue with the Card Header. When I add several buttons, they align in one row, but when I add a textbox, it always breaks into a new line. Can anyone provide assistance with this? I would like to have a ...

Exploring nested JSON through recursive iteration in JavaScript

Consider this JSON data containing comments that are fetched via AJAX call: json = 'comments': [ {'id':1,'parent':0}, {'id':2,'parent':1}, {'id':3,'parent':2}, {'id&apos ...

Using regular expressions to add a string before each occurrence

I have scoured numerous resources and forums but couldn't find a suitable solution for my problem. Since I am not well-versed in this topic, I am reaching out to the experts for assistance. This is the extent of what I have accomplished so far: This ...

Scrolling vertically without the appearance of a scrollbar

I've searched all over the internet for a solution to my problem, but nothing seems to work for me. I want to keep vertical scrolling ability while hiding the scrollbar from view in the y direction all the time. The challenge is to make my #content-m ...

The data source is having trouble connecting to the updated JSON format due to issues with pagination

I'm utilizing pagination.js to fetch asynchronous data using the code below. $('#demo').pagination({ dataSource: 'https://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?&ap ...