Edit: Well @NoviceDeveloper (excuse me, pun intended), (apparently this is perceived as potentially offensive, while it was just tongue-in-cheek speak).
Usually if you want to provide a value based on another observable, use a computed observable. For example, suppose the other observable string you wanted to append was a query string containing the file type, like so: 'filename.extension&type=filetype'
.
You could have the following basic KO VM:
var app = { input: ko.observable('') }; // this is your viewmodel
app.fileType = ko.computed(function() {
var val = this.input(),
extension = val.slice(val.lastIndexOf('.')+1).toLowerCase();
switch(extension) { // didn't include all, just an example
case 'png':
case 'jpeg':
case 'gif':
return 'image';
break;
case 'xml':
case 'json':
return 'data';
break;
default:
return 'unknown';
};
// you could also prefix the path with a known path, eg.
// return 'my/custom/path' + app.realInput();
}, app);
// the following function gets called on button click
app.submitFile = function() { $('#myForm').submit(); }
However, the file
input type is notorious for providing unreliable values in different browsers; eg Google Chrome will give you a value of C:/fakepath/yourfile.extension
while IE will include the entire, real path for example. So if you really want to use the file
input, you should strip this off, and have a hidden realInput
updated with the real value you need, like so:
<input type="hidden" name="file" id="file1hidden" data-bind="value: realInput" />
Where realInput
is a computed observable in which you add your appended string:
app.realInput = ko.computed(function() {
//replace dummmy string like Chrome's 'C:/fakepath'
var finalInput = app.input().replace(/.*[\/\\]/g,'');
finalInput = finalInput + '&type=' + app.fileType();
return finalInput;
}, app);
Test it here