Calculate Text Width with jQuery

This jQuery plugin should calculate the textwidth of elements.

jQuery.fn.textWidth = function(){
    var _t = jQuery(this);
    var html_org = _t.html();
        html_org = _t.val();
    var html_calcS = '' + html_org + '';
    var _lastspan = jQuery('span').last();
    //console.log(_lastspan, html_calc);
        'font-size' : _t.css('font-size')
        ,'font-family' : _t.css('font-family')
    var width =_lastspan.width() + 5;
    return width;

Mode of usage:


PS: Works on input elements too.

  • Catatan Belajar
    March 28, 2014 at 11:06 am

    Thank guy, helpful a lot.

  • zellina
    March 2, 2015 at 10:44 pm

    Sometimes it is very difficult to find good content on this topic. But your post is my way to desired information, my problem is solved now. Thanks for posting something worth knowledge

  • okhomeexteriors
    March 26, 2015 at 7:25 pm

    You actually make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

  • Business of Guam
    June 1, 2015 at 10:23 pm

    You may post on the professional credentials for the blog owner. You could express it’s outstanding. Your blog experience can springboard your click through.

    June 6, 2015 at 9:19 am

    Hey very nice web site!! Man .. Excellent .. Amazing .. its a really great blog post of i enjoyed lot.thanks again for share with us.

  • randy-houser
    June 9, 2015 at 5:57 pm

    You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!

Leave a comment

Your email address will not be published

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

− 2 = 5

{"type":"main_options","images_arr":"'#ffffff'","enable_ajax":"off","soundcloud_apikey":"","bg_isparallax":"off","bg_slideshow_time":"0","bg_transition":"slidedown","site_url":"http:\/\/","theme_url":"http:\/\/\/wp-content\/themes\/qucreative\/","blur_ammount":"26","width_column":"50","width_section_bg":"","width_gap":"30","border_width":"0","border_color":"#ffffff","translate_leave_a_comment":"Leave a comment","translate_leave_a_comment_to":"Leave a comment to","is_customize_preview":"off","width_blur_margin":"30","gallery_w_thumbs_autoplay_videos":"off","enable_native_scrollbar":"on","content_enviroment_opacity":"30","menu_enviroment_opacity":"70"}