Chuckun
Chuckun

Reputation: 37

Cannot get onchange to trigger innerHTML update

Ok I want my file upload button to change a span I have to make the user aware the file HAS been selected.

<script type="text/javascript">
function get_filename(obj,fileupid) {
    var file = obj.value;
    var disploc = fileupid.value;
    document.getElementById('current_file_' + disploc).innerHTML='Attached: ' + file;
}
</script>

...

<input type="file" id="file-503" onchange="get_filename(this,'file-503');" /> <span id="current_file_file-503">&nbsp;</span>

Can anyone see a problem with this code? The actual input and span IDs are variable but this is how they are being output in this case.

If I enter the innerHTML change directly into the address bar or scratchpad (putting 'file-503' in place of disploc and just using 'test' as the innerHTML output of course), then it works.. But in actual use, nothing happens..

I've been racking my brains for literally hours now and nothing is making sense.

Please help!!

Upvotes: 0

Views: 946

Answers (1)

adeneo
adeneo

Reputation: 318182

fileupid is an argument passed to the function, and it's a string, it has no value ?

function get_filename(obj,fileupid) {
    var file = obj.value;
    var disploc = fileupid;
    document.getElementById('current_file_' + disploc).innerHTML='Attached: ' + file;
}

FIDDLE

Upvotes: 1

Related Questions