EDIT
Ok, now I got it. Your JS should be this then:
$(function() {
$('#title').on('DOMSubtreeModified', function() {
var title = $('#title'),
planName = title.html().length;
// depending on html length, update font-size
if(planName > 30) {
title.css('font-size', '10px');
} else if(planName > 20) {
title.css('font-size', '12px');
} else if(planName > 10) {
title.css('font-size', '15px');
}
});
$('li').on('click', function() {
$('#title').text( $(this).text() );
});
});
The DOMSubtreeModified should fire whenever the content of the element changes (as this modifies the DOM Subtree). The 'click' part is not necessary, but I've kept it for testing purposes.
According to Dottoro, Opera doesn't support this event and IE 9 and below may not support or be buggy.
For more options and informations, check these other StackOverflow question:
By what you said, an user will "select a unit":
use case:
User selects a unit,
units have unique titles,
depending on title, resize font to prevent overflow
So, you need to change the font-size as part of this action. I used your code to make an example:
HTML
<ul>
<li>On load, font size is set depending on length of title.</li>
<li>However, I want this to be reactive...</li>
<li>Change the length of Title and hit run</li>
<li>Try clicking these itens</li>
<li>You will see</li>
<ul>
<br/><br/>
<span id="title">TEST TEST</span><br/>
JS
$(function() {
$('li').on('click', function() {
var titleEl = $('#title').text( $(this).text() ),
planName = titleEl.text().length;
// depending on text length, update font-size
if (planName > 30) {
titleEl.css('font-size', '10px');
} else if (planName > 20) {
titleEl.css('font-size', '12px');
} else if(planName > 10) {
titleEl.css ('font-size', '15px');
}
});
});
Try to click in the list itens. You see? As a response to "changing the unit", the title and font-size changes.
If this answer doesn't help you, give more details.