0

I have successfully created an HTML form that gets a file from a user and also allows giving a name for the file. I have also succeeded in creating a javascript function that can get the name of the file from the upload stream, and only the name (without the path and extension). Problem is, I somewhat do not manage to change the input that's there for renaming the upload like I want to. Here is the working function with two demonstrative alerts:

<script type="text/javascript">
function updateInput(val) {
    som = val.split('\\')[val.split('\\').length - 1];
    som = som.split('.')[0];
    alert('Hey 1');
    fileName.value = som;
    alert('Hey 2');
}; </script>

"Hey 1" alert is displayed and it's alright. "Hey 2" is actually not displayed! fileName is both the name and id of the input for the file's name, I DID try using document.getElementById('fileName').value = ... but with no luck.

Declaration of fileName:

<input type="text" runat="server" id="fileName" name="fileName" />

The javascript is located in the code under this input. The uploadImage input (which is type of file) is above this input. (Yes, the event is called and som gets the value I want it to have)

*I am trying to change the value of an input:text element. I will try the var thing.

THANK YOU IF YOU CAN HELP!

5
  • 1
    Can we see the html portion where the input field is declared? Commented Dec 17, 2012 at 19:41
  • Are you trying to set the value of an input:file element? Or am I misreading? Anyways, why do you expect fileName.value = som; to work? Commented Dec 17, 2012 at 19:55
  • Probably want to put var in front of som so it's declared as a local variable. Also, what is the fileName variable? It isn't declared here so we don't know what it is. Commented Dec 17, 2012 at 20:16
  • I am trying to change the value of an input:text element. I will try the var thing. I expect fileName.value = som; or document.GetElementById('fileName').value = som; to work, because I saw that around the internet and they had no complaints. Adding the declaration on the main message. Commented Dec 18, 2012 at 11:28
  • What is fileName variable? How do you get it? Commented Dec 18, 2012 at 11:38

1 Answer 1

1

As far as I understand your problem, code below should work fine:

<script type="text/javascript">
function updateInput(val) {
    var som = val.split('\\')[val.split('\\').length - 1];
    som = som.split('.')[0];
    alert('Hey 1');
    document.getElementById('<%=fileName.ClientID %>').value = som;
    alert('Hey 2');
}; </script>

Because of runat="server", id of an element in browser will be different from one you see in your .aspx file. Id of HTML element will be something like CotentPlaceholder1_fileName, so document.getElementById('fileName') will not find it and document.getElementById('fileName').value = son; will throw an error.

The same problem with fileName.value = som; - as far as I understand, you are trying to use feature of IE (not sure if it is IE only feature, I just never use it but remember that there is something like that) which creates variables in global namespace using IDs of elements. ID is different from fileName, so variable name is different also.

To get an id of an runat="server" element like it will be in browser, you can use .ClientID property of server side object.

Also, you should use var to define a variable inside a function scope. Like var som = val.split('\\')[val.split('\\').length - 1];. This way it will not be visible in global scope (outside of updateInput function). Otherwise you may potentially get problems with it in future

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It worked :) Hopefully I'll pass it on if I see someone having the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.