Connecting Tech Pros Worldwide Forums | Help | Site Map

JavaScript not working in IE and Safari, works in Firefox fine.

dpodkuik@gmail.com
Guest
 
Posts: n/a
#1: Feb 17 '06
I have a simple function that does submit for me:

<script language="javascript" type="text/javascript">
function sort()
{
//selected item value from the drop down list

var dud=window.document.statusForm.sortByStaff.value;
window.document.statusForm.action="status.cfm?sort By="+dud;
window.document.statusForm.method="post";
window.document.statusForm.submit();
}
</script>

I have several drop down boxes (<select> element) in which i set
onclick="sort()". Works perfectly in Firefox, and doesn't work in IE
and Safari. Also this same function works fine in all 3 if appended to
a button instead of select element. What am I doing wrong?


VK
Guest
 
Posts: n/a
#2: Feb 17 '06

re: JavaScript not working in IE and Safari, works in Firefox fine.



dpodkuik@gmail.com wrote:[color=blue]
> I have a simple function that does submit for me:
>
> <script language="javascript" type="text/javascript">
> function sort()
> {
> //selected item value from the drop down list
>
> var dud=window.document.statusForm.sortByStaff.value;
> window.document.statusForm.action="status.cfm?sort By="+dud;
> window.document.statusForm.method="post";
> window.document.statusForm.submit();
> }
> </script>
>
> I have several drop down boxes (<select> element) in which i set
> onclick="sort()". Works perfectly in Firefox, and doesn't work in IE
> and Safari. Also this same function works fine in all 3 if appended to
> a button instead of select element. What am I doing wrong?[/color]

....
<script type="text/javascript">
function sortBy(category) {

with(category.form) {
action="status.cfm?sortBy="+category;

// Are you switching from GET to POST ?
// Otherwise it is more reasonnable to indicate
// the form method once in the form itself
method="POST";

submit();
}
}
</script>
</head>
<body>
....
<form name="categoryForm" method="post" action="">
....
<select name="staff" onchange="sortBy(this[this.selectedIndex].value)">
....

dpodkuik@gmail.com
Guest
 
Posts: n/a
#3: Feb 17 '06

re: JavaScript not working in IE and Safari, works in Firefox fine.


I have simplified the function based on your advice, but still was only
able to obtain result in Firefox with onclick set instead of onchange.
When I use onchage nothing works anywhere.

function sortStaff(action)
{
window.document.statusForm.action="status.cfm?sort By="+action;
window.document.statusForm.submit();

}

<select name="staff"
onchange="sortStaff(this.options[this.selectedIndex].value)">

I am not switching from POST to GET so I just moved method into form
declaration from javascript.

VK
Guest
 
Posts: n/a
#4: Feb 17 '06

re: JavaScript not working in IE and Safari, works in Firefox fine.



dpodkuik@gmail.com wrote:[color=blue]
> I have simplified the function based on your advice[/color]

But you still prefer to use "lazy syntacs" instead of the proper one:
document.forms[...].elements[...]
OK, it's your code - but don't say you weren't alerted.

[color=blue]
> but still was only
> able to obtain result in Firefox with onclick set instead of onchange.[/color]

"onclick" is not promised to work with select elements. Select elements
do support guaranteed onfocus - onchange - onblur handlers.
[color=blue]
> When I use onchage nothing works anywhere.[/color]

Which means that you have some other error somewhere. Compare this
testcase with your page to find out where:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function sortStaff(action)
{
document.statusForm.action="status.cfm?sortBy="+ac tion;
alert(document.statusForm.action);
}
</script>

</head>

<body bgcolor="#FFFFFF">

<form method="post" action="" name="statusForm">
<select name="select1"
onchange="sortStaff(this[this.selectedIndex].value)">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
</body>
</html>

dpodkuik@gmail.com
Guest
 
Posts: n/a
#5: Feb 17 '06

re: JavaScript not working in IE and Safari, works in Firefox fine.


He he

Ok, i found all the dumb things I did. onchange doesn't work when
attached to options :). Moved to element declaration and everything
works fine.
IE doesn't support onclick and i guess safari doesn't either.

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Feb 17 '06

re: JavaScript not working in IE and Safari, works in Firefox fine.


dpodkuik@gmail.com wrote:
[color=blue]
> Ok, i found all the dumb things I did. onchange doesn't work when
> attached to options :). Moved to element declaration and everything
> works fine.[/color]

JFYI: /That/ is _not_ called an (element) declaration:

<select name="staff" onchange="...">

It is called the start tag of the `select' element.

This is called an element declaration:

<!ELEMENT SELECT - - (OPTGROUP|OPTION)+ -- option selector -->

It declares the `select' element (in the Document Type Definition)
so that you can use it in your markup later.


PointedEars
Randy Webb
Guest
 
Posts: n/a
#7: Feb 18 '06

re: JavaScript not working in IE and Safari, works in Firefox fine.


Thomas 'PointedEars' Lahn said the following on 2/17/2006 3:56 PM:[color=blue]
> dpodkuik@gmail.com wrote:
>[color=green]
>> Ok, i found all the dumb things I did. onchange doesn't work when
>> attached to options :). Moved to element declaration and everything
>> works fine.[/color]
>
> JFYI: /That/ is _not_ called an (element) declaration:
>
> <select name="staff" onchange="...">
>
> It is called the start tag of the `select' element.
>
> This is called an element declaration:
>
> <!ELEMENT SELECT - - (OPTGROUP|OPTION)+ -- option selector -->
>
> It declares the `select' element (in the Document Type Definition)
> so that you can use it in your markup later.[/color]

To the OP: Ignore Thomas. He thinks he knows more than anybody else but
he seldom does.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Closed Thread