Monica
Monica

Reputation: 1534

how do I get the width from a label?

This is my code, and every time I get null. Why??

$('label[for="name"]').width(); 

This label is created by validation jQuery plugin, so it only has a class error on it. So I need to specifically call this label, and get the width.

And this is my HTML:

<div class="input-field">
     <input type="text" name="prenom" id="name" 
            size="30" class="text-input-big error"                 
            placeholder="PRÉNOM">
     <label for="name" class="error">Le prénom est requis</label> 
</div>    

Upvotes: 0

Views: 2831

Answers (2)

Monica
Monica

Reputation: 1534

So the problem was solved by doing the following.

    $( window ).load(function() {
    $( "#form" ).submit(function( event ) {         
        var rightNameWidth = $('.input-field.name label').width(); 
        console.log(rightNameWidth);
        $('.name label').css('right', '-'+rightNameWidth+'px');
        event.preventDefault();
    });         
});

Was cause the DOM was not ready

Upvotes: 0

maembe
maembe

Reputation: 1300

Your code looks correct to me. Where are you calling this function? I did it like this:

$(document).ready(function(){
    alert($('label[for="name"]').width()); 
});

If you run it before the DOM is ready it will return 0

Upvotes: 2

Related Questions