set text field works on Safari not Firefox

I

ioneabu

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>
 
R

RobB

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;
}
 
M

Mick White

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]

}
 
I

ioneabu

RobB said:
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
 
T

Thomas 'PointedEars' Lahn

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?

The `type' attribute is missing, both if should be Valid HTML 4 and Valid
XHTML. Validate your markup before you complain: said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top