Reputation: 141
I'm trying to adjust title sizes depending on character length. (wordpress)
This is what I have so far. It doesn't return any error but it doesn't work.
jQuery(function ($) {
$(window).load(function () {
var textLength = $('.autotitle').val().length;
if (textLength < 20) {
// Do noting
} else if (textLength > 20) {
$('.autotitle').css('font-size', '16px');
}
});
});
Upvotes: 0
Views: 12945
Reputation: 56
Many times one would like to decrease font size (make it smaller) when text is long based on letter count (text might have been entered through some CMS or translated text)
$('.text').each(function(){
var el= $(this);
var textLength = el.html().length;
if (textLength > 20) {
el.css('font-size', '0.8em');
}
});
Upvotes: 4
Reputation: 270
If u use .autotitle
then changes will be applied to every element where u use .autotitle
as class
attribute ...
Instead that u can use id
attribute of particular element to apply changes to only that element
Ex :
$('#id').css('font-size','16px');
Upvotes: 0
Reputation: 141
solved it with a php function
function trim_title() {
$title = get_the_title();
$limit = "14";
if(strlen($title) <= $limit) {
echo $title;
} else {
echo '<span class="longtitle">'; echo $title; echo '</span>';
}
}
thanks to everyone.
Upvotes: 0