By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,848 Members | 3,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,848 IT Pros & Developers. It's quick & easy.

JS is working in firefox but not working in IE

P: 1
Hi All,

i am using this below addElement() function to create the file element dynamically

Expand|Select|Wrap|Line Numbers
  1. function addElement( filename,businessName ){
  2.  
  3. // Make sure it's a file input element
  4. //if( element.tagName == 'INPUT' && element.type == 'file' )
  5. {
  6. var textdiv = document.createTextNode('SER file to upload:'); 
  7. var new_element = document.createElement( 'input' );
  8. var divdetails =document.createElement('div');
  9. divdetails.id='files_div'+filecount
  10.  
  11. new_element.type = 'file';
  12. new_element.id='my_file_element'+filecount;
  13.  
  14.  new_element.setAttribute('name','testFile[' +  filecount  +  ']'); 
  15.  
  16. var treval = 'document.getElementById("files_div'+filecount+'").style.display="block"'; 
  17. var treval1 = 'document.getElementById("files_div'+filecount+'").style.position="absolute"'; 
  18. var treval2 = 'document.getElementById("files_div'+filecount+'").style.top="78px"'; 
  19.  
  20. filecount++
  21. var tableid = document.getElementById('tableid');
  22.  
  23. divdetails.appendChild(textdiv); 
  24.  
  25. divdetails.appendChild(new_element);
  26. tableid.appendChild(divdetails);
  27. //document.upload.appendChild(divdetails);
  28. document.upload.appendChild(new_hiddenelement);
  29. //document.getElementById("tempFiles").style.display="none";
  30. if(document.getElementById("files_div0") !=null)
  31. {
  32. document.getElementById("files_div0").style.visibility="hidden";
  33. }
  34. eval(treval);
  35. eval(treval1);
  36. eval(treval2);
  37.  
  38. };
  39.  
and using this below codei am retriving the value of dyamically created file

Expand|Select|Wrap|Line Numbers
  1. value_of_file = 'document.upload.elements["testFile[' +  count_list1 +  ']"].value'                
  2. value_of_file = eval(value_of_file);
  3.  
above code is working perfectly in teh firefox but in IE it was showing null value exception


please help to solve this problem..


thanks in advance....


Riaz
Oct 24 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Unfortunately, IE doesn't set the names of elements with setAttribute() or .name syntax when created dynamically. It needs its own non-standard syntax in createElement(). You may find this link useful for a cross-browser solution.
Oct 24 '08 #2

iam_clint
Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. var pinput = document.createElement("INPUT");
  2. pinput.setAttribute("id", "partnum");
  3. pinput.setAttribute("name", "partnum");
  4. pinput.id="partnum";
  5. pinput.name="partnum";
  6.  
This is some code I use for setting the names of created Elements without the IE hack... this seems to work in all browsers i've tested on.. ie6, ie7, firefox, google chrome.. It doesn't seem to work if the id attribute is not there.. If i remember correctly it doesn't seem to work if the id attribute isn't there first.

Now here comes the part that gets people confused and think that .name doesn't work.. IE only gets elements by name if they are native form elements... input, select, textarea so on

So I use this
Expand|Select|Wrap|Line Numbers
  1. function getAllElementsByName(name) {
  2.     var Elements=document.getElementsByTagName("*");
  3.     var Return_Array=Array();
  4.     var r=0;
  5.     for (var i=0; i<Elements.length; i++) {
  6.         if (Elements[i].getAttribute("name")==name) {
  7.             Return_Array[r]=Elements[i];
  8.             r++;
  9.         }
  10.     }
  11.     return Return_Array;
  12. }
  13.  
Oct 24 '08 #3

P: 55
correct me if I'm wrong clint, but IE doesn't allow setting the name attribute for grouped input items (like radio and check boxes) after creation, (or dynamically in 6 period) I've had to use the very irritating

Expand|Select|Wrap|Line Numbers
  1. document.createElement("<input type="+args.type+" name="args.name">")
method...

As far as I know, if you dynamically create form elements, there are some wierd caveats that ie refuses to make easier.

also why:

Expand|Select|Wrap|Line Numbers
  1.   var r=0;
  2.   for (var i=0; i<Elements.length; i++) {
  3.      if (Elements[i].getAttribute("name")==name) {
  4.         Return_Array[r]=Elements[i];
  5.         r++;
  6.   }
in that scenario don't 'i' and 'r' equal the same value?
Oct 24 '08 #4

iam_clint
Expert 100+
P: 1,208
r won't always be the same because i is incrementing with no if statements
r only increments when Elements[i].getAttribute("name")==name
Expand|Select|Wrap|Line Numbers
  1.   var r=0; 
  2.   for (var i=0; i<Elements.length; i++) { 
  3.      if (Elements[i].getAttribute("name")==name) { 
  4.         Return_Array[r]=Elements[i]; 
  5.         r++; 
  6.      }
  7.   } 
  8.  
as to the other question i'm not sure off the top of my head I would have to run some tests.
Oct 24 '08 #5

P: 55
sorry, should have read the code more thoroughly:p
Oct 24 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.