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>