Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript and FireFox / Chrome

Status
Not open for further replies.

JA3395

Programmer
May 22, 2007
88
IE
Has anyone meet this before

I have this Javascript function that is supposed to change a value before submitting the form.

The form name is dynamic, so is the value
Code:
function SubmitForm(What,DoWhat) {
  var f=document.getElementById(What);
  [COLOR=red]f.Task.value = DoWhat;[/color]   // <<<<<< FireFox fails on this
  f.submit();
}
later...
<form name="modForm1" action="etc">
<input type="button" onclick="Javascript:SubmitForm('modForm1','Modify');" value="Modify This Entry"/>
<input type="button" onclick="Javascript:SubmitForm('modForm1','Delete');" value="Delete This Entry"/>
</form>
<form name="modForm2" action="etc">
<input type="hidden" name="Task" value="" /> 
<input type="button" onclick="Javascript:SubmitForm('modForm2','Modify');" value="Modify This Entry"/>
<input type="button" onclick="Javascript:SubmitForm('modForm2','Delete');" value="Delete This Entry"/>
</form>

The code works fine in IE, but not in either of the other two browsers.

Does anyone have any idea how to get around this one?
I can't use getElementById, because that just gets the first Task, and I have multiple forms, all with the Element "Task".
 
I've firebugged this script and it's not the value that's the problem

Code:
function SubmitForm(What,DoWhat) {
[COLOR=red]  var f=document.getElementById(What); [/color]
  f.Task.value = DoWhat;
  f.submit();
}

f is equal to null after the getElementById. Why won't this work in FireFox?

Do I have to use some combination of eval() and the string to get this working?
 
OK, I've bashed myself upside my head and made a correction...

Code:
function SubmitForm(What,DoWhat) {
  var f=document.getElementById(What);
[COLOR=red]  f.Task.value = DoWhat;[/color]
  f.submit();
  return true;
}
later...
<form name="modForm1" id="modform1" action="etc">
<input type="button" onclick="Javascript:SubmitForm('modform1','Modify');" value="Modify This Entry"/>
<input type="button" onclick="Javascript:SubmitForm('modform1','Delete');" value="Delete This Entry"/>
</form>
<form name="modForm2" id="modform2"  action="etc">
<input type="hidden" name="Task" value="" />
<input type="button" onclick="Javascript:SubmitForm('modform2','Modify');" value="Modify This Entry"/>
<input type="button" onclick="Javascript:SubmitForm('modform2','Delete');" value="Delete This Entry"/>
</form>

I've got the getElementById working (the id was all lowercase), but the f.Task.Value is still failing.

FireBug isn't giving me an error, processing just stops.

Just a thought the Task is the name not the id, isn't it?
 
Hi

[tt]getElementsById()[/tt] gets the element by [tt]id[/tt]. You have no element with the [tt]id[/tt] you specified.
Code:
<form [red]id[/red]="modForm1" action="etc">
<input type="button" onclick="SubmitForm('modForm1','Modify');" value="Modify This Entry"/>
<input type="button" onclick="SubmitForm('modForm1','Delete');" value="Delete This Entry"/>
</form>
<form [red]id[/red]="modForm2" action="etc">
<input type="hidden" name="Task" value="" />
<input type="button" onclick="SubmitForm('modForm2','Modify');" value="Modify This Entry"/>
<input type="button" onclick="SubmitForm('modForm2','Delete');" value="Delete This Entry"/>
</form>
And do not use [tt]javascript:[/tt] pseudo-protocol when executing code in event handlers.
JA3395 said:
The code works fine in IE
That means nothing. Explorer not cares about standards.


Feherke.
 
OK, ignore me...

I had to get really specific. FireFox doesn't like the shortcuts

This works:

Code:
  f.elements['Task'].value = DoWhat;

Stoopid mozilla!!
 
Hi

JA3395 said:
FireFox doesn't like the shortcuts
FireFox has no problem referencing [tt]form[/tt] elements as object properties. There is no need to reference them as array items.
JA3395 said:
Stoopid mozilla!!
Do not blame a standard compliant browser just because refuses to interpret your invalid code the same way as you want.

Feherke.
 
FireFox has no problem referencing form elements as object properties. There is no need to reference them as array items.

This was the ONLY way FireFox would process the line, so, yes there is every reason to reference them as an array item. No other way (apart from using the numeric array element) works.

As for invalid code. It's not invalid, it's just not compliant to some arbitrary "standard", it's perfectly valid code for the language.
 
You don't even have the hidden form field of Task in the first form (in contrast with the second form). The rest of arguing, it's nil.
 
As for invalid code. It's not invalid, it's just not compliant to some arbitrary "standard", it's perfectly valid code for the language.

Good luck with your career serving fries in McDonalds.



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
As for invalid code. It's not invalid, it's just not compliant to some arbitrary "standard", it's perfectly valid code for the language.

All standards are "Arbitrary" they work because everyone agres to abide by them. failure to follow the accepted standard results in a reduced market share.

As javascript was invented by netscape (The code base for mozilla/Firefox) I would defer to these bowsers in any argument over what is correct operation.

if you are going to use 3rd party enhancements(?) do not be supprised if they do not work in other applications.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top