I want to change the extinsion of .doc to .txt.
alert(myvar) show empty alert.
enter code here
<form>
<input type="file" id="f1">
<button onclick="myFunction(f1.value)">Try it</button>
</form>
<script>
function myFunction(a) {
var mystr = a;
var myarr = mystr.split(".doc");
var ex= ".txt";
var myvar = myarr + ex;
alert(myvar);
}
</script>
use str.replace function
mystr.replace(".doc", ".txt")
function changeExt(s){
if(s.substring(s.length - 3,s.length) == "doc"){
var s = s.substring(0,s.length - 3);
s += "txt";
return s;
}
else{
return 'bad ext';
}
}
you have array whwn used split function . so
<script>
function myFunction(a) {
var mystr = a;
var myarr = mystr.split(".doc");
var ex= ".txt";
var myvar = myarr[0] + ex;
alert(myvar);
}
</script>
<script>
function myFunction(a) {
var mystr = a;
var myarr = mystr.split(".doc")[0];
var ex= ".txt";
var myvar = myarr + ex;
alert(myvar);
}
</script>
You can do the following
<input type="file" id="f1">
<input type="button" value="test" onclick="test(f1.value)">
function test(a){
var file = a;
file = file.split(".");
file = file[0]+".html"; //any extension you want
alert(file);
}
please try the given below code.also follow the FIDDLE DEMO
In html:
<input type="file" id="f1">
<input type="button" value="myfun" onclick="myFunction(f1.value)">
In js:
window.myFunction = function(a) {
var file = a;
file = file.split(".");
file = file[0]+".txt";
alert(file);
}
please try the given below code.also follow the FIDDLE DEMO
In html:
<input type="file" id="f1">
<input type="button" value="myfun" onclick="myFunction(f1.value)">
In js:
window.myFunction = function(fileName) {
var newFileName = fileName.substr(0, fileName.lastIndexOf("."));
newFileName = newFileName+".txt";
alert(newFileName);
}
why you choose spilit function when jquery provide us replace string and its variable too. here is example
var str = "Prabhash Rawat";
var res = str.replace("Rawat", "Huda");
after run this example the output of this script will be
Prabhash Huda