user1370161
user1370161

Reputation: 23

JQuery or Javascript code to change the font of the last character in html text

I know it sounds odd, but I'd like to create an html class that will change the font of the last character in the tagged text.

while css lets you change the fontsize, color of the first character of some strings, there is sadly nothing to change the font of the last character.

Is it best to use the charAt(length-1) in JS or is there some way in JQuery to alter the last character of the selected class?

Upvotes: 2

Views: 2226

Answers (2)

Denys Séguret
Denys Séguret

Reputation: 382150

You'll have to fetch, change, and then reput the html content of your texts.

I made a fiddle to illustrate it :

http://jsfiddle.net/dystroy/3maG5/

Be aware that there are risks : without greater analysis you may have problems if your paragraphs (or other texts) end with an element.

EDIT : I just see that eyurdakul made a few seconds before me a very similar answer. I think he's correct too. I let this answer as a complementary one to give a testable example. Of course, if one answer should be accepted, it's probably eyurdakul's one, he was faster :)

Upvotes: 1

eyurdakul
eyurdakul

Reputation: 912

give them some class for the selector, .last for example;

$(".last").each(function(){
     var inner = $(this).html();
     var firstPart = inner.substring(0, (inner.length-1));
     var secondPart = inner.substring((inner.length-1), inner.length);
     $(this).html(firstPart);
     $("<span/>").css("font", "Arial").html(secondPart).appendTo($(this));
});

Upvotes: 1

Related Questions