2

I have a div id="XYZ" with CSS specified width: 37%;

I then wish to use the px width of this div to set other widths, but I can only access the given css% value and not the computed px width of the div in the DOM. What am I doing wrong?

$( document ).on( "pagecreate", "#page", function( event ) {

        var divWidth = $("#XYZ").width();
        console.log('div XYZ width is ' + divWidth); 

});

It outputs 37. Which is the css % value, but jquery width() SHOULD give me the computed width in px, shouldn-t it?

I was thinking that maybe the problem is that I-m asking at 'pagecreate' (same result in deprecated 'pageinit').

How can I get the computed width of the div?

9
  • i think this link help you SEE stackoverflow.com/questions/17305707/… Commented Apr 26, 2014 at 10:21
  • 1
    @vivek, you are not telling me anything. Why do you think I need to read the api again? What am I not seeing? Commented Apr 26, 2014 at 10:35
  • 1
    use pagecontainershow (if you're using jQM 1.4) event to get computed width. At pagecreate event, elements are invisible and still page has not taken full shape. Commented Apr 26, 2014 at 11:51
  • 1
    Page is created but still hidden, not visible at this stage. It's visible when pagecontainershow fires. jqmtricks.wordpress.com/2014/03/26/jquery-mobile-page-events Commented Apr 26, 2014 at 22:28
  • 1
    @Omar, great link! Very nice graphical and text breakdown!! Commented Apr 26, 2014 at 22:36

2 Answers 2

1

Using pagecreate to retrieve actual .width(), .height(), .offest(), etc., won't return the computed value since page and elements within it are are all hidden.

To retrieve such data, you need to use pagecontainertransition when transition between previous page and next page is complete, or pagecontainerhide when previous page is completely hidden, or pagecontainershow when next page is completely visible.

However, note that those events can't be attached to a specific page. As of jQM 1.4, "page events" are replaced with "PageContainer Events". Thus, - unfortunately - you ought to use if statement or switch/case.

$( document ).on( "pagecontainershow" , function( event, ui ) {
  var divWidth = $( "#XYZ" ).width();
  console.log('div XYZ width is ' + divWidth);
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can use getComputedStyle

The Window.getComputedStyle() method gives the values of all the CSS properties of an element after applying the active stylesheets and resolving any basic computation those values may contain.

Use

var divWidth = document.defaultView.getComputedStyle($("#XYZ")[0], null).width;

Or

var divWidth = window.getComputedStyle($("#XYZ")[0], null).width;

1 Comment

THANKS, however I am still getting the 37% value for both. The problem may well lie with JQM order of page events.

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.