0

My question is quite simple:

I want to store the FILE NAME that I select using file input, and using jQuery I want to put that name somewhere else instantly without having the page refreshed.

Maybe I'd replace a simple text with the filename. How would I do that?

Example:

The text in the beginning: Hello World

After selecting the file, the "Hello World" would be replaced with "my_filename.gif".

Ideas?

2 Answers 2

1

You can save it in data function against the element:

function getFileNameFromPath(path) {
        var ary = path.split("\\");
        return ary[ary.length - 1];
    }

    $(function () {

        $('input[type=file]').change(function () {
            $(this).data(
                'fileName',
                getFileNameFromPath($(this).val())
                );

            alert($(this).data('fileName'));
        });

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

Comments

0

Here's a potential solution:

<input type="file" onchange="$('#helloWorld').val($(this).val());"/>
<input id="helloWorld" value="Hello World"/>

You can check it out here

EDIT:

Modified it so the value was set to "Hello World" initially rather than the innerHTML.

7 Comments

Mixing javascript logic in mark-up is bad practice and should be avoided
@XGreen In general, I agree with you. However, I am a firm believer that it is OK to add JavaScript events to elements in their markup. This makes the event handlers clear when viewing the HTML, and if they are named appropriately, in the JavaScript file as well. If I was doing something more complex, I would have made an onchange function in a JavaScript file and referenced that. However, I think that adds unneeded complexity in this case and makes the connection between the inputs more ambiguous.
What happens when you want to change the functionality of a certain element. Or better yet, remove it? Now you have to access two files, remove a function, get rid of the event handler? You need separation. This also allows for improvement on accessibility. You'll be challenged to make your document accessible via the document itself without javascript. HTML should be able to survive on its own without the aid of Javascript (and of course, CSS).
@XGreen I would say in the case where the element's event handlers may change, you are right that the event declarations should be removed from the HTML to avoid confusion. As to the HTML being able to survive on its own, it will still render the same when the JavaScript events are unavailable, but probably with better JavaScript errors that would help debug why the page is not completely functional.
@davin Not sure what you are getting at. Is there a good reason to go for "<input ..></input>" over "<input ../>"?
|

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.