JQM adjust label width

157 views Asked by At

We want to adjust all label-tags from a jquery mobile form with the with from the largest label. Here one codesample for one field:

... <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>...

This is the jquery function:

$.fn.labelWidth = function () {
    var maxWidth = 0;
    $('fieldset').each(function() {
        $(this).find('label').each(function() {
            var width = $(this).width();
            if (width > maxWidth) {
                maxWidth = width;
            }
        });
    });
    $('fieldset').each(function() {
        $(this).find('label').each(function() {
            $(this).css({width:maxWidth});
        });
    });
}

... and this is the function-call:

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

If we debug: Into the variable maxWith we have the right width ...but the form dosn't change? What is our mistake?

1

There are 1 answers

0
Gajotres On

You were close but not close enough. Your plugin is made on assumption that every label has a separate width and that is not correct. Label has always 20% width, input has 78% width and there's a margin of 2% between them.

Working example: http://jsfiddle.net/Gajotres/mMmcP/

You changed plugin code:

$.fn.labelWidth = function () {
    // This will override preset 20% width
    $('.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');
        });      
    });
}

One other plugin is important for this plugin to work, it can be found here. I am not a second plugin developer.

Here's a second plugin:

// 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 rule.test(this.cssText) ? 'important' : '';
    }
}

// Escape regex chars with \
RegExp.escape = function(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

// The style function
jQuery.fn.style = function(styleName, value, priority) {
    // DOM node
    var node = this.get(0);
    // Ensure we have a DOM node 
    if (typeof node == 'undefined') {
        return;
    }
    // CSSStyleDeclaration
    var style = this.get(0).style;
    // Getter/Setter
    if (typeof styleName != 'undefined') {
        if (typeof value != 'undefined') {
            // Set style property
            var priority = typeof priority != 'undefined' ? priority : '';
            style.setProperty(styleName, value, priority);
        } else {
            // Get style property
            return style.getPropertyValue(styleName);
        }
    } else {
        // Get CSSStyleDeclaration
        return style;
    }
}