I have given text strings which are displayed in containers of varying width. I want to allow linebreaks only after the character "·", which occurs two or three times in each of those text strings.
What I came up with (see below): With jQuery, I wrapped a span around the whole string with a css class that applies white-space: nowrap;
to the string, and additionally I added a <br>
tag after each "·", both using a replaceAll
function: Now line breaks can only happen at the position of the inserted <br>
tags.
My problem: This forces line breaks at all <br>
tags. But if part one and two of the text string (i.e. the text up to the second "·" character) would fit into the parent container next to each other, I would like the line break only to happen after the second "·"!
var mytext = $('#wrapper2 .string1').text();
var search = " ·";
$('#wrapper2 .string1').each( function(index, element) {
$(element).html( $(element).html().replaceAll(mytext, '<span class="inner_wrapper">' + mytext + '</span>') );
$(element).html( $(element).html().replaceAll(search, search + '<br>') );
})
.wrapper1 {
width: 400px;
border: 1px solid #aaa;
padding: 0.5em;
font-size: 18px;
text-align: center;
}
#wrapper2 .inner_wrapper {
white-space: nowrap;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>The original text</h3>
<div id="wrapper1" class="wrapper1">
<p class="string1">The title of the event · day, month and year · at the end the location</p>
</div>
<p>The width of the above box can change. What I want: Linebreaks should only occur <b>after the "·" characters</b>.</p>
<h3>The text processed by jQuery</h3>
<div id="wrapper2" class="wrapper1">
<p class="string1">The title of the event · day, month and year · at the end the location</p>
</div>
<p>This comes close, but if there is enough space (as in the boxes above), the first linebreak should occur after the <b>second</b> "·" character, not after <em>all</em> of them.</p>
`. As an alternative the HTML Unicode entity `` is feasible.
based on the widths and the box width. – Nikkorian Dec 13 '22 at 23:52