set text field works on Safari not Firefox

Discussion in 'Javascript' started by ioneabu@yahoo.com, Apr 27, 2005.

  1. Guest

    I am trying to do the basic task of setting a text field from the
    choice made from a select box. I learned how to code it from my
    O'Reilly Javascript reference which is a few years old. The code works
    as expected in Safari but not Firefox which is where I really need it
    to work primarily. Of course, I would like it to work everywhere. I
    would greatly appreciate it if someone could point out my error. I did
    check to make sure that all javascript functionality is enabled in
    Firefox. Thank you!

    wana

    The code (generated by the perl module CGI.pm):

    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"
    xml:lang="en-US"><head><title>Untitled Document</title>
    <script>
    function set_text()
    {
    myform.mytext.value =
    myform.myscroll.options[myform.myscroll.selectedIndex].text;

    }

    </script>
    </head><body>
    <form method="post" action="/./s.pl"
    enctype="application/x-www-form-urlencoded" name="myform">

    <select name="myscroll" size="7" onchange="set_text()">
    <option value="laboratory report">laboratory report</option>
    <option value="specialist consult note">specialist consult
    note</option>
    <option value="hospital discharge summary">hospital discharge
    summary</option>
    <option value="ER discharge">ER discharge</option>
    <option value="medical records">medical records</option>
    <option value="prescriptions">prescriptions</option>
    <option value="audio dictation">audio dictation</option>
    </select>
    <br />
    <input type="text" name="mytext" />
    <div>
    <input type="hidden" name=".cgifields" value="myscroll" />
    </div>
    </form>
    </body></html>
     
    , Apr 27, 2005
    #1
    1. Advertising

  2. RobB Guest

    wrote:
    > I am trying to do the basic task of setting a text field from the
    > choice made from a select box. I learned how to code it from my
    > O'Reilly Javascript reference which is a few years old. The code

    works
    > as expected in Safari but not Firefox which is where I really need it
    > to work primarily. Of course, I would like it to work everywhere. I
    > would greatly appreciate it if someone could point out my error. I

    did
    > check to make sure that all javascript functionality is enabled in
    > Firefox. Thank you!
    >
    > wana
    >
    > The code (generated by the perl module CGI.pm):
    >
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <!DOCTYPE html
    > PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"
    > xml:lang="en-US"><head><title>Untitled Document</title>
    > <script>
    > function set_text()
    > {
    > myform.mytext.value =
    > myform.myscroll.options[myform.myscroll.selectedIndex].text;
    >
    > }
    >
    > </script>
    > </head><body>
    > <form method="post" action="/./s.pl"
    > enctype="application/x-www-form-urlencoded" name="myform">
    >
    > <select name="myscroll" size="7" onchange="set_text()">
    > <option value="laboratory report">laboratory report</option>
    > <option value="specialist consult note">specialist consult
    > note</option>
    > <option value="hospital discharge summary">hospital discharge
    > summary</option>
    > <option value="ER discharge">ER discharge</option>
    > <option value="medical records">medical records</option>
    > <option value="prescriptions">prescriptions</option>
    > <option value="audio dictation">audio dictation</option>
    > </select>
    > <br />
    > <input type="text" name="mytext" />
    > <div>
    > <input type="hidden" name=".cgifields" value="myscroll" />
    > </div>
    > </form>
    > </body></html>


    The script engine is unable to resolve the reference to 'myform' as
    it's neither a global variable (in good browsers) nor local to the
    function. You'll need to prepend document. to the reference to qualify
    it properly (document.forms. would be even better).

    Since you're calling that function from the Select.onchange property,
    with the Select object no farther away than the 'this' keyword, why not
    pass it (along with its .form property)?

    <select name="myscroll" size="7" onchange="set_text(this)">

    function set_text(obj)
    {
    var el;
    if (el = obj.form.elements.mytext)
    el.value = obj.options[obj.selectedIndex].text;
    }
     
    RobB, Apr 27, 2005
    #2
    1. Advertising

  3. Mick White Guest

    wrote:

    > I am trying to do the basic task of setting a text field from the
    > choice made from a select box. I learned how to code it from my
    > O'Reilly Javascript reference which is a few years old. The code works
    > as expected in Safari but not Firefox which is where I really need it
    > to work primarily.


    [snip]
    >
    > The code (generated by the perl module CGI.pm):


    > function set_text()
    > {
    > myform.mytext.value =
    > myform.myscroll.options[myform.myscroll.selectedIndex].text;
    > }
    >
    > </script>


    function set_text(){
    var f=document.myForm;
    f.mytext.value =
    f.myscroll.options[f.myscroll.selectedIndex].text;
    }
    Mick

    [snip]

    }
     
    Mick White, Apr 27, 2005
    #3
  4. Guest

    RobB wrote:
    > wrote:
    > > I am trying to do the basic task of setting a text field from the
    > > choice made from a select box. I learned how to code it from my
    > > O'Reilly Javascript reference which is a few years old. The code

    > works
    > > as expected in Safari but not Firefox which is where I really need

    it
    > > to work primarily. Of course, I would like it to work everywhere.

    I
    > > would greatly appreciate it if someone could point out my error. I

    > did
    > > check to make sure that all javascript functionality is enabled in
    > > Firefox. Thank you!
    > >
    > > wana
    > >
    > > The code (generated by the perl module CGI.pm):
    > >
    > > <?xml version="1.0" encoding="iso-8859-1"?>
    > > <!DOCTYPE html
    > > PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > > <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"
    > > xml:lang="en-US"><head><title>Untitled Document</title>
    > > <script>
    > > function set_text()
    > > {
    > > myform.mytext.value =
    > > myform.myscroll.options[myform.myscroll.selectedIndex].text;
    > >
    > > }
    > >
    > > </script>
    > > </head><body>
    > > <form method="post" action="/./s.pl"
    > > enctype="application/x-www-form-urlencoded" name="myform">
    > >
    > > <select name="myscroll" size="7" onchange="set_text()">
    > > <option value="laboratory report">laboratory report</option>
    > > <option value="specialist consult note">specialist consult
    > > note</option>
    > > <option value="hospital discharge summary">hospital discharge
    > > summary</option>
    > > <option value="ER discharge">ER discharge</option>
    > > <option value="medical records">medical records</option>
    > > <option value="prescriptions">prescriptions</option>
    > > <option value="audio dictation">audio dictation</option>
    > > </select>
    > > <br />
    > > <input type="text" name="mytext" />
    > > <div>
    > > <input type="hidden" name=".cgifields" value="myscroll" />
    > > </div>
    > > </form>
    > > </body></html>

    >
    > The script engine is unable to resolve the reference to 'myform' as
    > it's neither a global variable (in good browsers) nor local to the
    > function. You'll need to prepend document. to the reference to

    qualify
    > it properly (document.forms. would be even better).
    >
    > Since you're calling that function from the Select.onchange property,
    > with the Select object no farther away than the 'this' keyword, why

    not
    > pass it (along with its .form property)?
    >
    > <select name="myscroll" size="7" onchange="set_text(this)">
    >
    > function set_text(obj)
    > {
    > var el;
    > if (el = obj.form.elements.mytext)
    > el.value = obj.options[obj.selectedIndex].text;
    > }



    Thank you! It makes sense now. I cannot blame my old book for my
    mistake, just myself for not paying attention. Thanks for the
    information and insight. And thanks to all for helpful responses.

    wana
     
    , Apr 27, 2005
    #4
  5. wrote:

    > I am trying to do the basic task of setting a text field from the
    > choice made from a select box. [...] The code works
    > as expected in Safari but not Firefox which is where I really need it
    > to work primarily. [...]
    > The code (generated by the perl module CGI.pm):
    >
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <!DOCTYPE html
    > PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"
    > xml:lang="en-US">


    XHTML 1.0 Transitional is voodoo. If you don't want clean markup, why are
    you using XHTML anyway? There is nothing in your document that requires
    it, especially not the deprecated elements and attributes. Use Valid HTML
    4.01 Transitional or Strict (I recommend the latter) instead.

    > <head>
    > <title>Untitled Document</title>

    ^^^^^^^^^^^^^^^^^
    That's a joke, yes?

    > <script>


    The `type' attribute is missing, both if should be Valid HTML 4 and Valid
    XHTML. Validate your markup before you complain: <http://validator.w3.org/>

    > function set_text()
    > {
    > myform.mytext.value =
    > myform.myscroll.options[myform.myscroll.selectedIndex].text;
    > }


    Replace with

    function set_text(f)
    {
    var o = f.elements["myscroll"];
    f.elements["mytext"].value = o.options[o.options.selectedIndex].text;
    }

    > </script>
    > </head><body>
    > <form method="post" action="/./s.pl"

    ^^^^^^^
    That's voodoo. "/s.pl" will suffice.

    > [...]
    > <select name="myscroll" size="7" onchange="set_text(this.form)">


    Replace with

    <select name="myscroll" size="7" onchange="set_text(this.form)">


    PointedEars
     
    Thomas 'PointedEars' Lahn, May 8, 2005
    #5
    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. JDeats
    Replies:
    2
    Views:
    582
    JDeats
    Sep 11, 2007
  2. Replies:
    4
    Views:
    258
    Randy Webb
    Feb 18, 2006
  3. Replies:
    11
    Views:
    210
    GĂ©rard Talbot
    Apr 28, 2006
  4. Terry
    Replies:
    1
    Views:
    123
    Terry
    Oct 20, 2007
  5. anne001
    Replies:
    9
    Views:
    315
    Thomas 'PointedEars' Lahn
    Dec 1, 2007
Loading...

Share This Page