Solved

setAttribute or writeAttribute in IE

Posted on 2010-09-14
13
878 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
Comment Utility
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
Comment Utility
<<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
Comment Utility
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
 

Author Closing Comment

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

Expert Comment

by:gurvinder372
Comment Utility
why grade C?
0
 

Author Comment

by:strimp101
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
<<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
Comment Utility
mplungjan that is actually similar to what I did, thank you.

gurvinder372 thank you for the explanation.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now