passing width in <div> style to javascript

Discussion in 'Javascript' started by John, Sep 11, 2011.

  1. John

    John Guest

    Here's some Javascript

    var ff=document.getElementById('one').style.width;
    alert(ff);

    and this is my <div>

    <div id='one' style='width:200px'>testing</div>

    It does not work. Is there anyway I can pass the width value to the
    javascript?

    Regards
    John
     
    John, Sep 11, 2011
    #1
    1. Advertising

  2. 11.9.2011 12:39, Andrew Poulos wrote:

    > I suspect you are running your code before the element exists.


    Sounds probable.

    Such problem can easily be recognized by looking at the console error
    log - and if someone doesn't know how to find it in his favorite
    browser, it's time to learn! If that takes too long, there's always the
    possibility of wrapping the JavaScript code into...

    try {
    // all the code here
    } catch(e) {
    alert(e);
    }

    Browsers are mostly silent about JavaScript errors because there are so
    many of them. But in program development, there is hardly any reason not
    to look at error messages!

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Sep 11, 2011
    #2
    1. Advertising

  3. John

    dhtml Guest

    On Sep 11, 3:30 am, "Jukka K. Korpela" <> wrote:
    > 11.9.2011 12:39, Andrew Poulos wrote:
    >
    >  > I suspect you are running your code before the element exists.
    >
    > Sounds probable.
    >
    > Such problem can easily be recognized by looking at the console error
    > log - and if someone doesn't know how to find it in his favorite
    > browser, it's time to learn!


    There was once an FAQ hosted on jibbering.com/faq that listed how to
    enable error reporting for various browsers. SInce we don't have an
    FAQ now, we're all (including the OP) SOL. Sorry, buddy. Try and
    figure out how to turn on error reporting in your browser. Get a
    debugger, too.


    If that takes too long, there's always the
    > possibility of wrapping the JavaScript code into...
    >
    > try {
    >    // all the code here
    >
    > } catch(e) {
    >    alert(e);
    > }
    >

    You can do that, but won't get anything meaningful out of IE versions;
    just "[object Error]". e.name, e.message, but really, what you want is
    all the information, including the line number. Turn on error
    reporting in your browser.

    > Browsers are mostly silent about JavaScript errors because there are so
    > many of them.


    So many errors, you mean? That's a reason users would want to hide
    them, sure.

    But in program development, there is hardly any reason not
    > to look at error messages!
    >

    Right. Turn on error reporting in your browser. Find the error
    console. Get a debugger.
    --
    Garrett
     
    dhtml, Sep 11, 2011
    #3
  4. Sun, 11 Sep 2011 10:51:23 -0700 (PDT), /dhtml/:

    > There was once an FAQ hosted on jibbering.com/faq that listed how to
    > enable error reporting for various browsers. SInce we don't have an
    > FAQ now, we're all (including the OP) SOL.


    Hm, is it really gone?

    http://jibbering.com/faq/#javascriptErrors

    Seems to open for me.

    --
    Stanimir
     
    Stanimir Stamenkov, Sep 11, 2011
    #4
  5. John

    John Guest

    "Andrew Poulos" <> wrote in message
    news:...
    > On 11/09/2011 6:18 PM, John wrote:
    >> Here's some Javascript
    >>
    >> var ff=document.getElementById('one').style.width;
    >> alert(ff);
    >>
    >> and this is my<div>
    >>
    >> <div id='one' style='width:200px'>testing</div>
    >>
    >> It does not work. Is there anyway I can pass the width value to the
    >> javascript?
    >>
    >> Regards
    >> John
    >>
    >>
    >>

    > Try
    >
    > window.onload = function() {
    > var ff = document.getElementById('one').style.width;
    > window.alert(ff);
    > };
    >
    > as I suspect you are running your code before the element exists.
    >
    > Andrew Poulos


    Hi

    Yes - you're righht. I had placed the line before the function I was
    calling.

    Many thanks,

    Regards
    John
     
    John, Sep 12, 2011
    #5
  6. In comp.lang.javascript message <94611152-7ef1-4c32-bf64-3e36a7a1908a@f2
    4g2000prb.googlegroups.com>, Sun, 11 Sep 2011 10:51:23, dhtml
    <> posted:

    >There was once an FAQ hosted on jibbering.com/faq that listed how to
    >enable error reporting for various browsers. SInce we don't have an
    >FAQ now, we're all (including the OP) SOL. Sorry, buddy. Try and
    >figure out how to turn on error reporting in your browser. Get a
    >debugger, too.


    There IS a FAQ hosted at jibbering (seen 2011-09-12 17:29 GMT approx).
    There is a copy on my site at $clj-faq.htm.
    There is a small amount in my js-debug.hrm.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE8 FF3 Op12 Sf5 Cr12
    news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
    <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Sep 12, 2011
    #6
  7. On 9/11/2011 11:51 AM, dhtml wrote:
    > There was once an FAQ hosted on jibbering.com/faq that listed how to
    > enable error reporting for various browsers. SInce we don't have an
    > FAQ now, we're all (including the OP) SOL.


    In response to the server(s) that host the FAQ sporadically acting up,
    I've taken to hosting my own copy of the C.L.J. FAQ. It's about 90-95%
    complete, only missing example pages and a few orphaned notes sections.

    I will continue to pull pages from the old FAQ when I find the time (and
    if I'm able to access it).

    Here's the FAQ on my host:
    http://www.fortybelow.ca/hosted/comp-lang-javascript/faq/

    --
    Matt McDonald: Web/Flash Developer—Edmonton, Alberta, Canada
     
    Matt McDonald, Sep 17, 2011
    #7
  8. John

    J.R. Guest

    Hi,
    Your code won't return the correct width for an element.

    Try this one:

    function getWidth(elem) {
    var elemWidth;
    elemWidth = elem.scrollLeft + elem.clientWidth;
    if (elemWidth < elem.scrollWidth) { elemWidth = elem.scrollWidth; }
    }

    Also see:
    <http://www.w3.org/TR/cssom-view/#extensions-to-the-element-interface>
    <http://msdn.microsoft.com/en-us/library/ms530302(v=VS.85).aspx>
    <http://www.quirksmode.org/dom/w3c_cssom.html>
     
    J.R., Sep 17, 2011
    #8
  9. In comp.lang.javascript message <j50puu$9uv$>, Fri, 16
    Sep 2011 18:34:38, Matt McDonald <> posted:

    >On 9/11/2011 11:51 AM, dhtml wrote:
    >> There was once an FAQ hosted on jibbering.com/faq that listed how to
    >> enable error reporting for various browsers. SInce we don't have an
    >> FAQ now, we're all (including the OP) SOL.

    >
    >In response to the server(s) that host the FAQ sporadically acting up,
    >I've taken to hosting my own copy of the C.L.J. FAQ. It's about 90-95%
    >complete, only missing example pages and a few orphaned notes sections.
    >
    >I will continue to pull pages from the old FAQ when I find the time
    >(and if I'm able to access it).
    >
    >Here's the FAQ on my host: http://www.fortybelow.ca/hosted/comp-lang-
    >javascript/faq/



    That alleges itself to be a real copy of Garrett's FAQ as at 2010-10-08.
    If, as you say, it is not a complete and accurate copy, you should
    remove it immediately.

    It seems a complete waste of effort, since, as I have repeatedly written
    here, I have a complete copy (without the horrible CSS) of Garrett's
    last version at my $clj-faq.htm.

    I also have some alternate entries, AISB, at js-faq-u.htm, and a version
    annotated with some of the suggestions made here at js-faq-a.htm.

    I've not recently found <http://jibbering.com/faq/> to be unavailable,
    I've just accessed it.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Sep 18, 2011
    #9
  10. John

    SAM Guest

    Le 19/09/11 00:48, Andrew Poulos a écrit :
    > On 19/09/2011 4:29 AM, Dr J R Stockton wrote:
    >
    >> I've not recently found<http://jibbering.com/faq/> to be unavailable,
    >> I've just accessed it.

    >
    > I just tried and couldn't get to it.
    >
    > Andrew Poulos



    maybe a little bit long to load ... 103 ko !


    --
    Stéphane Moriaux avec/with iMac-intel
     
    SAM, Sep 19, 2011
    #10
  11. In comp.lang.javascript message <kbSdndwHH_Ok6OvTnZ2dnUVZ_jWdnZ2d@westne
    t.com.au>, Mon, 19 Sep 2011 08:48:20, Andrew Poulos
    <> posted:

    >On 19/09/2011 4:29 AM, Dr J R Stockton wrote:
    >
    >> I've not recently found<http://jibbering.com/faq/> to be unavailable,
    >> I've just accessed it.

    >
    >I just tried and couldn't get to it.


    It's working for me now. Have any other Australians tried it?

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
     
    Dr J R Stockton, Sep 20, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndrewF
    Replies:
    1
    Views:
    810
    Bruce Barker
    Oct 10, 2005
  2. =?Utf-8?B?cm9kY2hhcg==?=

    how to: div width = browser.width?

    =?Utf-8?B?cm9kY2hhcg==?=, Oct 27, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    542
    =?Utf-8?B?cm9kY2hhcg==?=
    Oct 30, 2006
  3. Steve Richter
    Replies:
    2
    Views:
    7,957
    Steve Richter
    Jun 15, 2007
  4. tomek milewski
    Replies:
    6
    Views:
    1,315
    tomek milewski
    Jun 25, 2007
  5. K Viltersten

    <div ... /> and <div ...></div>

    K Viltersten, Mar 30, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    777
Loading...

Share This Page