jQM - LabelWidth plugin

by Dragan Gaić

HTML

<link rel="stylesheet" href="http://jeromeetienne.github.com/jquery-mobile-960/css/jquery-mobile-fluid960.css">
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script src="http://imakewebthings.github.com/jquery-waypoints/waypoints.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>
            
            <div data-role="content">
                <form id="kontoman-form">
                    <fieldset data-role="fieldcontain">
                        <label for="fusrName">Benutzername/E-Mail<b class="kontoman-mandatory">*</b></label>
                        <input id="fusrName" name="fusrName" type="text" placeholder="Benutzername/E-Mail" value=""/>
                    </fieldset>
                    <fieldset data-role="fieldcontain">
                        <label for="fusrName">Polizzennumer<b class="kontoman-mandatory">*</b></label>
                        <input id="fusrName" name="fusrName" type="text" placeholder="Benutzername/E-Mail" value=""/>
                    </fieldset>                       
                </form>
            </div>
        </div>    
    </body>
</html>

JavaScript

$(document).on('pageshow', '#index', function(){ 
   $('#kontoman-form').labelWidth();    
});

$.fn.labelWidth = function () {
    $('.ui-input-text').style('width', 'auto', 'important');
    
    var maxWidth = 0;
    var contentWidth = $('.ui-content').width();
    $('fieldset').each(function() {
        $(this).find('label').each(function() {
            var width = $(this).width();
            if (width > maxWidth) {
                maxWidth = width;
            }
        });
    });
    
    var finalLabelWidth = Math.ceil((maxWidth/contentWidth)*100);
    var finalInputWidth = 100 - finalLabelWidth - 2;
    
    $('fieldset').each(function() {
        $(this).find('label').each(function() {
            $(this).style('width', finalLabelWidth+'%', 'important');
            $(this).next().style('width', finalInputWidth+'%', 'important');
        });      
    });
}

// For those who need them (< IE 9), add support for CSS functions
var isStyleFuncSupported = CSSStyleDeclaration.prototype.getPropertyValue != null;
if (!isStyleFuncSupported) {
    CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
        return this.getAttribute(a);
    };
    CSSStyleDeclaration.prototype.setProperty = function(styleName, value, priority) {
        this.setAttribute(styleName,value);
        var priority = typeof priority != 'undefined' ? priority : '';
        if (priority != '') {
            // Add priority manually
            var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*' + RegExp.escape(value) + '(\\s*;)?', 'gmi');
            this.cssText = this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
        } 
    }
    CSSStyleDeclaration.prototype.removeProperty = function(a) {
        return this.removeAttribute(a);
    }
    CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
        var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?', 'gmi');
        return...