reading style attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabian

    #1

    reading style attributes

    How can I read the object.style.di splay attribute? With teh following:

    var x = object.style.di splay;

    I always get a result of undefined. Solutions?


    --
    --
    Fabian
    Visit my website often and for long periods!
    Ikuti pembaruan skor langsung dan analisis statistik pertandingan sepak bola terkini yang disajikan secara interaktif di AGAM69.


  • Martin Honnen

    #2
    Re: reading style attributes



    Fabian wrote:
    [color=blue]
    > How can I read the object.style.di splay attribute? With teh following:
    >
    > var x = object.style.di splay;
    >
    > I always get a result of undefined. Solutions?[/color]

    If you set
    <a id="aLink" style="display: block;"
    href="http://JavaScript.FAQT s.com/">...</a>
    then I am sure browsers like Netscape 6/7, IE5/5.5/6, Opera 7 allow you
    to read
    document.getEle mentById('aLink ').style.displa y
    and that returns the string
    'block'
    However if the inline style property is not set e.g.
    <a id="a2ndLink" href="http://Javascript.FAQT s.com">
    then
    document.getEle mentById('a2ndL ink').style.dis play return the empty string
    ''
    as no value is set.
    If you want the computed value check

    document.defaul tView.getComput edValue(documen t.getElementByI d('a2ndLink'),
    '').getProperty Value('display' )
    in Netscape 6/7 and Opera 7, with IE 5/5.5/6 you need
    document.getEle mentById('a2ndL ink').currentSt yle.display

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Fabian

      #3
      Re: reading style attributes

      Martin Honnen hu kiteb:
      [color=blue]
      > Fabian wrote:
      >[color=green]
      >> How can I read the object.style.di splay attribute? With teh
      >> following: var x = object.style.di splay;
      >>
      >> I always get a result of undefined. Solutions?[/color]
      >
      > If you set
      > <a id="aLink" style="display: block;"
      > href="http://JavaScript.FAQT s.com/">...</a>
      > then I am sure browsers like Netscape 6/7, IE5/5.5/6, Opera 7 allow
      > you to read
      > document.getEle mentById('aLink ').style.displa y
      > and that returns the string
      > 'block'[/color]


      I tried all your methods, and this is the only one that worked (ie6). I
      was wondering though, is there a particular reason why it cant read or
      infer the default style values?


      --
      --
      Fabian
      Visit my website often and for long periods!
      Ikuti pembaruan skor langsung dan analisis statistik pertandingan sepak bola terkini yang disajikan secara interaktif di AGAM69.


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: reading style attributes

        "Fabian" <lajzar@hotmail .com> writes:

        [setting style attribute and reading element.style][color=blue]
        > I tried all your methods, and this is the only one that worked
        > (ie6). I was wondering though, is there a particular reason why it
        > cant read or infer the default style values?[/color]

        The "style" object on an element is directly related to the style
        attribute in the HTML tag. That is why
        <a style="display: block" id="foo">...</a>
        makes
        document.getEle mentById("foo") .style.display
        equal "block", but using a style sheet doesn't.

        To find the *actual* style value used, you must find the computed
        style. W3C DOM compliant browsers can do that, as was shown, using
        the getComputedStyl e method:

        var elem = document.getEle mentById("foo") ;
        var style = document.defaul tView.getComput edStyle(elem,"" );
        alert(style.dis play);

        For IE (which is not very W3C DOM compliant) has its own way
        of reading the computed style:

        var elem = document.getEle mentById("foo") ;
        var style = elem.currentSty le;
        alert(style.dis play);

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...