2

Demo page for testing: http://jsfiddle.net/rCKzs/

<script type="text/javascript">
    $(document).ready(function() {  
        alert( $("#test").width() );          
    });
</script>

..

<div id="test">content</div>

If I remove the jQuery Mobile, then it is okay, otherwise it return 0.

1 Answer 1

2

Do not use document.ready with jQM:

Important: Use pageInit(), not $(document).ready()

The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event. This event is explained in detail at the bottom of this page.

Also jQM only supports jQuery 1.6.4

I would use the live() like this

$('div').live('pageshow',function(event, ui) {
    alert($("#test").width());  
});
Sign up to request clarification or add additional context in comments.

1 Comment

For my specific issue when binding to pageinit my div still gave a height of 0, but binding to .live('pageshow',... (as suggested) worked great.

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.