2

I'm trying to set an element width using jQuery and it just won't work.

Here is my code:

var $padding = 30,
$oddWidth = parseInt( Math.round( $(window).width()*0.75 ) ),
$odd = $oddWidth-$padding;

$('div:jqmData(role="content")').css({ 'margin-left':'25%'}).width( $odd );

Can anybody help me out?

3
  • This is working for me in my quick jQuery.mobile test. What exactly is not working and are you seeing any errors in the console? Commented Jul 21, 2011 at 11:13
  • It's working for me! jsfiddle.net/Dg5qV/2 Maybe it is your selector? Commented Jul 21, 2011 at 11:14
  • There's no errors in the console and the margin-left 25% is assigned. So it can't be init- or selector-related. Also if I'm using width('123px') or width('75%') they are also assinged. I don't get why it cannot be a variable. Commented Jul 21, 2011 at 11:25

1 Answer 1

2

The following is working for me, so are you wrapping the jQuery code inside the following?

$(document).ready(function() {
    $("div:jqmData(role='page')").live('pageshow',function(){
        var $padding = 30,
        $oddWidth = parseInt( Math.round( $(window).width()*0.75 ) ),
        $odd = $oddWidth-$padding;
        $('div:jqmData(role="content")').css({ 'margin-left':'25%'}).width( $odd );
    });
});

jQuery.mobile has a different document.ready() for when a "page" is fully loaded, so perhaps without this, the width() is not correct or the <div data-role="content"> does not exist yet.

Sign up to request clarification or add additional context in comments.

1 Comment

Found it... the code was inside an if-else statement and I declared both the variables inside the if part and did this formatting in the else-part... :-| Still, THANKS!, because copy-pasting your code made me recognize it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.