DOM/HTML Button Element

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

    DOM/HTML Button Element


    [This was submitted as a bug to Opera; I'm posting it here, after not
    seeing mention of this in a newsgroup search]:

    In HTML 4.01, a <button> element can take 1 of 3 types:

    type=submit (default)
    type=reset
    type=button

    The Javascript to create a button element dynamically is

    elem = document.create Element('button ');
    elem.type = 'reset';

    Running this code in Opera 7.54 gives the message:

    "DOMExcepti on: NO_MODIFICATION _ALLOWED_ERR"

    My intuition is that the official Opera.com response will be that this
    is as required by the DOM spec, which lists "type" as a readonly property.
    And I'll give you that, but how can the spec be correct? It refers to the
    HTML 4.01 spec, which lists the 3 possible values for type; but the DOM
    interface shows no other method for altering the value of type.

    In other words, how do I use Javascript to create the equivalent of either

    <button name="rset" type="reset">Re set Button</button>
    or
    <button name="push" type="button">P ush Button</button>

    ??

    For what it's worth, Mozilla makes the "type" property read/write; and
    IE requires its own bizarre argument to createElement (as:

    elem = document.create Element('<butto n type="reset" value="Reset">R eset</button>')


    hj
  • Michael Winter

    #2
    Re: DOM/HTML Button Element

    On 08/06/2005 00:47, Howard Jess wrote:
    [color=blue]
    > [This was submitted as a bug to Opera;[/color]

    It isn't a bug. Just some oddness in the DOM specification.

    [snip]
    [color=blue]
    > In HTML 4.01, a <button> element can take 1 of 3 types:[/color]

    Unfortunately, the BUTTON element is all but useless due to IE's woeful
    inability to use it properly. You'd be better off using the INPUT
    element, which has a modifiable type property.

    [snip]
    [color=blue]
    > My intuition is that the official Opera.com response will be that this
    > is as required by the DOM spec, which lists "type" as a readonly property.
    > And I'll give you that, but how can the spec be correct?[/color]

    You should ask that on the W3C mailing lists.

    Notice that in DOM 2 HTML, the INPUT element's type attribute was
    modified from read-only to read/write, but the BUTTON element was not.
    Either it was overlooked, or there was a reason.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Howard Jess

      #3
      Re: DOM/HTML Button Element

      [color=blue][color=green]
      >> [This was submitted as a bug to Opera;[/color]
      >
      > It isn't a bug. Just some oddness in the DOM specification.[/color]

      Right; the "oddness" makes dynamicly created button elements a lot
      less useful, though; and in Mozilla this "oddness" is ignored (type
      is read/write), and in Safari, although the property isn't directly
      modifiable, setAttribute('t ype',...) has the expected effect.

      [color=blue]
      > [snip]
      >[color=green]
      >> In HTML 4.01, a <button> element can take 1 of 3 types:[/color]
      >
      > Unfortunately, the BUTTON element is all but useless due to IE's woeful
      > inability to use it properly. You'd be better off using the INPUT
      > element, which has a modifiable type property.[/color]

      Yes and no. If you see you're running in IE (by detecting its "woeful"
      behavior, of course), there are measures you can take.

      [color=blue]
      > You should ask that on the W3C mailing lists.[/color]

      OK, good advice.

      [color=blue]
      > Notice that in DOM 2 HTML, the INPUT element's type attribute was
      > modified from read-only to read/write, but the BUTTON element was not.
      > Either it was overlooked, or there was a reason.[/color]

      Bummer. Does nobody use this stuff?


      hj

      Comment

      • Martin Honnen

        #4
        Re: DOM/HTML Button Element



        Howard Jess wrote:
        [color=blue][color=green][color=darkred]
        >>>[This was submitted as a bug to Opera;[/color]
        >>
        >>It isn't a bug. Just some oddness in the DOM specification.[/color]
        >
        >
        > Right; the "oddness" makes dynamicly created button elements a lot
        > less useful, though; and in Mozilla this "oddness" is ignored (type
        > is read/write),[/color]

        At least for <input> elements I remember that originally Mozilla
        followed the readonly for the type property specified in DOM Level 1
        until someone file a bug complaining that that disallows dynamically
        creating the different input types. It is likely that for <button>
        elements the approach was similar.
        So the problem is indeed with the DOM spec but of course browsers should
        follow common sense and allow dynamical creation of the different types
        of input and button elements, after all the DOM should allow you to
        programmaticall y create anything that static HTML allows.


        --

        Martin Honnen

        Comment

        • Hallvord R. M. Steen

          #5
          Re: DOM/HTML Button Element

          On Wed, 08 Jun 2005 01:47:38 +0200, Howard Jess
          <howard..@dhite chnologies.dot. com> wrote:
          [color=blue]
          > [This was submitted as a bug to Opera; I'm posting it here, after not
          > seeing mention of this in a newsgroup search]:[/color]
          [color=blue]
          > The Javascript to create a button element dynamically is
          >
          > elem = document.create Element('button ');
          > elem.type = 'reset';
          > Running this code in Opera 7.54 gives the message:
          >
          > "DOMExcepti on: NO_MODIFICATION _ALLOWED_ERR"[/color]

          Good catch, thanks: the specification probably should be corrected to
          allow you to set type. I have confirmed your bug report. As you mentioned
          we're probably "correct" spec-wise but when the specs are inconsistent..
          --
          Hallvord R. M. Steen
          Opera Software
          Faster, safer and smarter than default browsers. Fully-featured for privacy, security, and so much more. Get the faster, better Opera browser for free.

          Comment

          • Csaba Gabor

            #6
            Re: DOM/HTML Button Element

            Howard Jess wrote:[color=blue]
            > The Javascript to create a button element dynamically is
            > elem = document.create Element('button ');
            > elem.type = 'reset';[/color]
            ....[color=blue]
            > <button name="rset" type="reset">Re set Button</button>
            >
            > For what it's worth, Mozilla makes the "type" property read/write; and
            > IE requires its own bizarre argument to createElement (as:
            >
            > elem = document.create Element('<butto n type="reset" value="Reset">R eset</button>')[/color]

            Interesting post. I didn't try this, but what about cloning an
            existing button element (of the type you desire), and then modifying
            the remaining elements?

            Also, I use button, when possible (ie. browser dependent), because that
            way I can indicate the access key by underlining it. e.g. <button ...
            accessKey=o><u> O</u>K</button>. I seem to recall that you have to be
            careful about how that text changes in some browsers. Possibly it is
            necessary to nest the text in a div? I forget, but just beware if you
            are doing things like
            Mor<u>e</u> >> changing to/from
            L<u>e</u>ss <<

            Csaba Gabor from Vienna

            Comment

            • Howard Jess

              #7
              Re: DOM/HTML Button Element



              Csaba Gabor wrote:
              [color=blue]
              > Howard Jess wrote:[color=green]
              >> The Javascript to create a button element dynamically is
              >> elem = document.create Element('button ');
              >> elem.type = 'reset';[/color]
              > ...[color=green]
              >> <button name="rset" type="reset">Re set Button</button>
              >>
              >> For what it's worth, Mozilla makes the "type" property read/write; and
              >> IE requires its own bizarre argument to createElement (as:
              >>
              >> elem = document.create Element('<butto n type="reset" value="Reset">R eset</button>')[/color]
              >
              > Interesting post. I didn't try this, but what about cloning an
              > existing button element (of the type you desire), and then modifying
              > the remaining elements?[/color]

              I'm not sure how you would do this; what do you mean by "an existing
              button element"? What if I'm creating the entire page using Javascript?

              In any case, I do have a (not very clean) workaround: a combo function that
              asks for element type (and name, which has a similar problem in IE) all
              at one time; this lets me code it as (for brevity here, no error checking):

              function createButton(ty pe,name) {
              var div = document.create Element('div');
              div.innerHTML=' <button type="'+type+'" name="'+name+'" ></button>';
              return div.firstChild;
              }

              I'd rather not use the non-standard innerHTML, but it's pretty well-
              supported by all DOM-ish browsers, so ...

              hj

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: DOM/HTML Button Element

                Howard Jess wrote:
                [color=blue]
                > Csaba Gabor wrote:[color=green]
                >> Howard Jess wrote:[color=darkred]
                >>> For what it's worth, Mozilla makes the "type" property read/write; and
                >>> IE requires its own bizarre argument to createElement (as:
                >>>
                >>> elem = document.create Element('<butto n type="reset"
                >>> value="Reset">R eset</button>')[/color]
                >>
                >> Interesting post. I didn't try this, but what about cloning an
                >> existing button element (of the type you desire), and then modifying
                >> the remaining elements?[/color]
                >
                > [...] What if I'm creating the entire page using Javascript?[/color]

                Then you would have far more serious problems than this.


                PointedEars
                --
                When you have eliminated all which is impossible, then
                whatever remains, however improbable, must be the truth.
                -- Sherlock Holmes

                Comment

                Working...