0

Is there a way to get available space between text and element boundaries?

The problem is next. I need to display an error message in input label (by design). If there is not enough space I need to show an Icon that can be hovered and display an error.

enter image description here

Is there a way to achieve this?

Sergey
  • 7,184
  • 13
  • 42
  • 85

1 Answers1

0

So if this is your element:

<div style="width:500px;" id="d">Birth settlement</div>

Put the text inside a span and calculate parent length - child length.

document.getElementById('l').textContent = document.getElementById('d').offsetWidth - document.getElementById('s').offsetWidth;
<div style="width:500px;" id="d"><span id="s">Birth settlement</span></div>
<span id="l"></span>
zomega
  • 1,538
  • 8
  • 26
  • The problem here is that `Birth settlement` will contain an error and this will affect the result. That's why I asked about text and not element – Sergey Aug 05 '19 at 15:05
  • I don't understand. My snippet gives you the width of the text. – zomega Aug 11 '19 at 08:37
  • Your text is fixed sized meanwhile mine may already contain an error. Inside the same tag. – Sergey Aug 11 '19 at 12:09