Determine the height of a block of text and then adjust it

I'm looking to add an HTML element inside a paragraph every 200 pixels of height to create a break. Here's the paragraph:

<div class="newspaper">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
</div>

.newspaper p {
    column-count: 3;
}

Within the paragraph, I want to inject a span tag like this:

<div class="newspaper">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</span>
 Nam liber tempor cum soluta nobis eleifend option congue nihil i...</p>
</div>

This is what I have attempted:

var height = $('.newspaper').height();
console.log(height);
if (height > 200) {
$( ".newspaper p" ).append( "</p><span>TEST</span><p>" );
}

The issue is that "append" adds the html at the end. I am trying to make it loop every 200px of the paragraph height.

Answer №1

There isn't a direct way to create 200 pixel intervals on a paragraph, but you can manually set blocks as 200px tall elements and develop a method to divide a block of text into chunks of N characters (where N is an optimal number for a 200px element).

var fullParagraph = `...`;
var N = 10; //N. chars in 1 chunk, used in a string slicing fn.

var parentDiv = document.createElement("div");

var TEST = document.createElement("span");
TEST.innerHTML = "TEST";

var iterations = Math.floor(fullParagraph.length / N);

for(let i = 0; i < iterations; i++){

//Insert your portion of the paragraph
let 200px_H_p = document.createElement("p");
const newContent = document.createTextNode( fullParagraph.slice(i * N, (i+1)*N ));
200px_H_p.appendChild(newContent);

//Now that the previous element is definitely 200px, insert TEST after it

parentDiv.appendChild(200px_H_p);
parentDiv.appendChild(TEST);

}

Essentially, I've divided the string into N-character chunks and added them to the parent Div in intervals along with your filler content (in-between elements).

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

What could be the reason for the initial response appearing blank?

How can I retrieve all the comments of a post using expressjs with mongodb? I am facing an issue where the first response is always empty. Below is the code snippet: const Post = require("../models/posts"), Comment= require("../model ...

Adjustable height for Material UI tab components

I encountered an issue that I initially thought was related to the general configuration of my app and the height I assigned to my page wrapping classes. However, after creating a simple out-of-the-box material UI tab example, it became clear that this beh ...

Ensuring accurate birth date validation using JavaScript

Can someone help me with validating a date of birth? I've tried using the new Date method to ensure that the date entered is not after today's date. However, even when I enter a date after today, it still doesn't show as an invalid date. var ...

How can I create a responsive input group with automatic width using Bootstrap 3.1?

I am having trouble with the layout of my two floating divs. The first div contains buttons and the second div contains an input-group. However, the input-group is moving down a line instead of utilizing the available space. Here is a link for reference: ...

The click event listener only seems to fire on the second attempt

This block of code is designed to function as a search algorithm that also provides keyword suggestions. When a user clicks on a keyword, it is supposed to be placed into an input textbox. The possible keywords are "first" and "second". However, there is ...

The Jquery/Javascript transitions on the Nike Snowboarding website are unparalleled

I'm curious if there is a Jquery plugin available for achieving the transition featured on . Specifically, the transition when clicking on the names. If anyone knowledgeable in Javascript/jQuery could guide me through how this can be achieved, I would ...

Utilizing Mongoose to Link Models with the Require Method

My goal is to update one collection called Prize, and then based on the result of that update, modify another collection named League. The code I currently have is as follows: app.post('/auth/prize/:prizeId', function(req, res) { console.lo ...

Exploring the Google Maps API Search feature

I am currently developing a JavaScript application that interfaces with the Google Maps API. The program has the following requirements: Enable users to input a location. Upon clicking the "find" button, convert the user's entered location into long ...

How can I navigate through all the divs by setting an interval slider with a random starting point?

I have a Jsonp request that returns a block of HTML with the following structure: <div id="slider_wrapper" style=""> <div style="xxx" ><section style="xx">xx</section></div> <div style="xxx" ><section style=" ...

Increase the padding on the left side of a background image

UPDATE: Solution discovered at this link thanks to Mr. Alien I am attempting to create a heading that resembles the following Title ------------------------------------------------- I have an image that I intend to use as a backdrop for it. However, I& ...

Can you tell me the locations of the src/js and build/js directories?

Just starting out and seeking guidance. I am currently working with Node v4.2.1 and Gulp 3.9.0 on a Windows 7 machine, following along with a tutorial to familiarize myself with the task runner Gulp. I'm attempting to concatenate tasks but I seem to ...

Is there a way to display the # symbol instead of characters in the input text field?

I have a text input field in a PHP page with a maxlength attribute set to 11. This input field is specifically used for entering phone numbers. The requirement is that when the user types a phone number, it should display as "#" symbols for all 11 characte ...

retrieve information based on specific criteria in strapi

I am currently developing an API using Strapi and I've encountered a specific situation where I need to fetch data. The requirement is to retrieve records where the audience_name is similar to '%audienceName%' and created_by is equal to 4 ...

Exploring the integration of Construct 3 objects with ReactJs

Although I am well-acquainted with Construct 3 platform, I now have an interest in website development. Specifically, I would like to incorporate a character-like object into my web project that moves similar to a game character. While I know how to achiev ...

I aim to adjust the width of a logo and ensure its size is fixed for optimal visibility

The selected logo needs to be more visible. I have attempted to use media queries to adjust its visibility, but I am having trouble with it. I want the logo to be able to stretch in width, but I am unsure how to achieve that. Currently, this is what I hav ...

ERROR: The data has reached its end prematurely (data length = 0, requested index = 4). Could this be a corrupted zip file?

Currently, I am developing a WhatsApp bot and storing the chat data in an Excel file using exceljs for further processing. In order to handle responses efficiently, I am utilizing promises to resolve them. Below is the function I have created to read the c ...

The unfortunate timing of the multi-material design lite snackbar

I am currently working on customizing notifications that appear when a Symfony entity is successfully updated. What I have implemented so far can be found here : var messagesTypes = { "notice": ["This is a green NOTICE"], "error": ["This is a red E ...

Crafting an animated experience using the SX attribute in Material-UI version 5

Currently, I am working on developing an animated spinner using SVG as a loader. After browsing online, I came across examples that utilize Styled Components for this purpose; however, it seems that approach is no longer supported. Do any of you have alte ...

Modifying column array properties using jsstore

I am working with a jsstore table called tblInvoice const tblInvoice: ITable = { name: "invoice", columns: { // Here "Id" is name of column id: { autoIncrement: true, primaryKey: true, notNull: false }, branchId: { ...

Concealing the footer on a webpage embedded within an iframe

<script> $(document).ready(function(){ $('.mydivclass').hide(); }); </script> Looking to embed Microsoft Forms in my HTML using an iframe. Can the footer of the Microsoft Forms be removed? ...