innerHTML sometimes reverts to previous value after function return

Discussion in 'Javascript' started by Warren Sarle, Jun 1, 2006.

  1. Warren Sarle

    Warren Sarle Guest

    The page http://warrensarle.com/temp.html has three lines of text.

    The top one is a p element saying "Not in a form",
    which is not in a form.

    The middle one is a button element saying "Button in a form",
    which is in a form.

    The bottom one is a div element saying "Original text".

    Both the p element and the button element are given the same
    function as an onclick handler. This function assigns a string,
    "Testing innerHTML", to the innerHTML property of the div element,
    and then calls alert to say it is done.

    When the button is clicked, the text in the div changes to
    "Testing innerHTML" as expected. But after dismissing the
    alert box, the text in the div reverts to "Original text"
    when using Firefox or Opera. The text does not revert when
    using IE.

    Nor does the text revert when the p element is clicked in any
    of these browsers, unless you subsequently click the button,
    which causes "Testing innerHTML" to revert to "Original text"
    when using Firefox or Opera.

    Could someone please explain why the text reverts in some
    situations but not others?

    --

    Warren S. Sarle SAS Institute Inc. The opinions expressed here
    SAS Campus Drive are mine and not necessarily
    (919) 677-8000 Cary, NC 27513, USA those of SAS Institute.
     
    Warren Sarle, Jun 1, 2006
    #1
    1. Advertisements

  2. Warren Sarle

    web.dev Guest

    Warren Sarle wrote:
    > The page http://warrensarle.com/temp.html has three lines of text.
    >
    > The top one is a p element saying "Not in a form",
    > which is not in a form.
    >
    > The middle one is a button element saying "Button in a form",
    > which is in a form.
    >
    > The bottom one is a div element saying "Original text".
    >
    > Both the p element and the button element are given the same
    > function as an onclick handler. This function assigns a string,
    > "Testing innerHTML", to the innerHTML property of the div element,
    > and then calls alert to say it is done.
    >
    > When the button is clicked, the text in the div changes to
    > "Testing innerHTML" as expected. But after dismissing the
    > alert box, the text in the div reverts to "Original text"
    > when using Firefox or Opera. The text does not revert when
    > using IE.
    >
    > Nor does the text revert when the p element is clicked in any
    > of these browsers, unless you subsequently click the button,
    > which causes "Testing innerHTML" to revert to "Original text"
    > when using Firefox or Opera.
    >
    > Could someone please explain why the text reverts in some
    > situations but not others?
    >


    If you check W3C, http://www.w3.org/TR/html4/interact/forms.html#h-17.5
    , by default, a button element by default is a submit. It appears FF
    and Opera honor this while IE does not. Some solutions:

    1. explicitly give the button a type
    2. handle the onsubmit event on the form so it does not submit the
    form.
     
    web.dev, Jun 1, 2006
    #2
    1. Advertisements

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. Nehmo Sergheyev
    Replies:
    1
    Views:
    702
    Andrew Urquhart
    May 9, 2004
  2. Marcin Vorbrodt

    ::std sometimes needed, sometimes not

    Marcin Vorbrodt, Sep 16, 2003, in forum: C++
    Replies:
    24
    Views:
    1,117
    Jerry Coffin
    Sep 17, 2003
  3. Replies:
    1
    Views:
    661
    gkelly
    Nov 29, 2006
  4. Randy Smith
    Replies:
    2
    Views:
    681
    Randy Smith
    Apr 24, 2007
  5. Stef Mientki
    Replies:
    0
    Views:
    323
    Stef Mientki
    Oct 18, 2008
  6. Sébastien Barthélemy

    __file__ is sometimes absolute, sometimes relative

    Sébastien Barthélemy, Oct 1, 2010, in forum: Python
    Replies:
    1
    Views:
    791
    Arnaud Delobelle
    Oct 1, 2010
  7. paul reed

    Custom Control Width sometimes has a value...sometimes doesn't

    paul reed, Feb 17, 2004, in forum: ASP .Net Building Controls
    Replies:
    8
    Views:
    351
    Jeffrey Tan[MSFT]
    Feb 21, 2004
  8. Jesse Schoch
    Replies:
    2
    Views:
    290
    Jesse Schoch
    Oct 2, 2003
Loading...