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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input type=file cant set name

Status
Not open for further replies.

techkate

Programmer
Feb 23, 2004
118
US
I am creating file inputs ad-hoc. However, the name I specify is not 'sticking'. The inputs that I create at design time behave with the name attribute appropriately.

note: the variable filesrc is passed into the routine as something like "testdoc2"
Code:
thisFile = document.createElement("input");
thisFile.type = "file";
thisFile.name = filesrc;
docTab.nextSibling.appendChild(thisFile);

In the script debugger, when I debug.print thisFile.name, it returns "testdoc2" properly. However, when I debug.print thisFile.outerHTML, I can see that there is no name attribute in it, as opposed to the inputs created at design time. Am I using the wrong attribute?

Thanks in advance.

Kate

[small]"The Distorted View"
-the only podcast that's fresh perked
www.distortedview.com[/small]

 
Hi Kate, inputs of type file cannot be changed by javascript. Image you have an input of type file that asks the user to upload a file to the webserver. And then using an onsubmit handler you change the value right before the form is submitted and point it to a different file on their computer that contains personal information. Because of this security risk, that value is readonly.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
I'm not changing it per se, I'm simply creating it.

Upon ad-hoc creation, I can set the id perfectly, and what seems to be any other attribute I need, but why not the name attribute?

How can I correct this problem? Should I just create it by changing its parents innerhtml += to the html string I need?

I'll give something like this a try:

Code:
thisParent.innerHTML += "<input type='file' name='"+filesrc+"'>";


Kate

[small]"The Distorted View"
-the only podcast that's fresh perked
www.distortedview.com[/small]

 
Ahh... you're just wanting to change the name. I see. Using the innerHTML solution is unnecessary. Here's an example that shows it is possible using both the name property and the setAttribute method:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

window.onload = function () {
   var a = document.getElementById("blah");
   alert(a.name);
   a.name = "newName";
   alert(a.name);
   a.setAttribute("name", "anotherNewName");
   alert(a.name);
};

</script>
<style type="text/css"></style>
</head>
<body>

<input type="file" id="blah" name="" />

</body>
</html>

Now... as to your specific problem, I'm not sure exactly why it's not getting set. It appears that you're trying to set the name equal to the value that's in a variable called filesrc. Are you meaning to set the name to the actual string "filesrc"? If that's the case, then make sure to encapsulate it in quotes:
Code:
thisFile = document.createElement("input");
thisFile.type = "file";
thisFile.name = [!]"filesrc"[/!];
docTab.nextSibling.appendChild(thisFile);
If not, then I would suggest using alert to see what's in the filesrc value so that you have an idea of what you're attempting to set the name to:
Code:
thisFile = document.createElement("input");
thisFile.type = "file";
[!]alert(filesrc);[/!]
thisFile.name = filesrc;
docTab.nextSibling.appendChild(thisFile);

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
No, filesrc is a very short basic string. Already checked that. Even if I just put a string like "testname1" or something like that it still doesn't work.

The setattribute method did not work. Same symptoms as my original code - thisFile.name returns the proper value, but thisFile.outerHTML does not include the name.

I must reference this input field in other routines, and it still breaks in the same spots.

For now I have to stick to the innerHTML. That works.

Kate

[small]"The Distorted View"
-the only podcast that's fresh perked
www.distortedview.com[/small]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top