To determine the pixel length of a string in JavaScript and jQuery, we can put the string in a span and then use the jQuery width
method to determine the width of the span.
For instance, we can write the following HTML:
<span></span>
Then we can write:
const span = document.querySelector('span')
span.innerText = 'Here is my string'
console.log($(span).width())
to get the span width document.querySelector
.
And then we set innerText
to the string of the span.
Finally, we log the span’s width that we get from $(span).width()
from jQuery.
The console log should log 111.