Solved

setAttribute or writeAttribute in IE

Posted on 2010-09-14
13
899 Views
Last Modified: 2012-05-10
Greetings,

I have a form in which the user can either upload a file containing a list of widgets or manually enter the widgets into a text field. If the user manually enters the widgets into the text field, I use AJAX to serialize the form elements, send the data to the server, and process the response. If the user manually uploads a file I use an iframe to simulate a file upload with AJAX.

The issue is that the button to submit the form must only submit the form if the user is uploading a file of widgets. Therefore the input button must be of type "submit". The form must not be submitted if the user is manually entering the widgets in a text field. Therefore the input button must be of type "button" (to prevent the form from submitting) because the processing takes place on the page with theform.

The solution I came up with is depending on whether the user uploads the file or manually enters data, I use (or would like to use) Prototype JS's writeAttribute method to change the type attribute from "submit" (for the file upload) to "button" (for the manual entry) on the button to submit the request.

Of course IE has seizures with the writeAttribute and setAttribute methods.

Questions:

1. is there perhaps a better way to change the submission control of the form?
2. What method is cross-browser compatible to change the "type" attribute of an input tag and the "action" attribute of a form tag?

Cheers!
0
Comment
Question by:strimp101
  • 5
  • 4
  • 3
13 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33674103
1. I would think it was setAttribute and getAttribute
2. simply return false in the onSubmit:

<form action="someaction.php" onSubmit="ajax(this); return false">
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33674403
<<is there perhaps a better way to change the submission control of the form?>>
You can submit the form like this

document.forms[0].action = ""; //mention the server side script name
document.forms[0].submit();

so the button can be of type 'button' only and still form can be submit through javascript
0
 

Author Comment

by:strimp101
ID: 33674598
I ended up keeping the input type as button and adding the action to the form. I used form.submit() to submit the form once we realized that it was supposed to be a form submission.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:strimp101
ID: 33674605
All I ended up needing was the submit() function.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33674616
why grade C?
0
 

Author Comment

by:strimp101
ID: 33677433
C is for an average answer. All gurvinder372 did was point out a method I temporarily overlooked. If you want "grade inflation" I'll change it to an A. Why offer A, B, C in the first place?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33679133
Your question was <<is there perhaps a better way to change the submission control of the form?>>
what else did you expected me to tell you other than submit() function?
Let me know what else were you expecting. It will help me in helping you better in future
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33679450
I would still surmise that my suggestion may a better one depending on the script you use to send the data.
0
 

Author Comment

by:strimp101
ID: 33681606
Bottom line is that gurvinder aided me in solving my problem. I should have just provided an A in the first place. I'm a trial member and not sure the politics/benefit of As v. Bs, etc. Thanks for the help...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33681808
Hi Strimp, Welcome to EE.

For some of us, a "B" is expected for a link to a working solution that helps you fix your issue and an "A" for actual code. The more code/Information YOU provide, the more complete solutions you will receive. For example I still do not know enough of your setup to judge my own suggestion.

That is why we expect you to provide more information when prompted and we can help you better.

Re-reading the question makes me think you want this:

<script type="text/javascript">
function validate(theForm) {
  if (theForm.widgetUploadField.value!="") {
    return true; // submit the form normally
  }
  iframeOrAjax(theForm); // instead use the iframe
  return false; // cancel the normal form submission
}
</script>
<form onSubmit="return validate(this)">
<input type="file" name="widgetUploadField" value="" />
.
.
<input type="submit" />
</form>
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33683328
<<I'm a trial member and not sure the politics/benefit of As v. Bs, etc. Thanks for the help...>>
No need to worry about politics of A's and B's. Believe me, there are plenty of experts in EE and experts like me are not irreplaceable.
I think the intention of Moderator is not to tell you that you need to give A's to get into good books of Experts, or be nicer to Experts (by giving them more points). But just to assert to you about how to give points (pardon my language if i am not articulate enough).
My intention was just to reason you about the grade and to know what was missing in my answer. Especially when you mentioned that <<All I ended up needing was the submit() function.>> #33674605
0
 

Author Comment

by:strimp101
ID: 33693775
mplungjan that is actually similar to what I did, thank you.

gurvinder372 thank you for the explanation.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question