I have an issue with the following code. I want to replace a string by another one, and generate an img code. However, I got an error message: str.replace is not a function
. Any idea why?
<input type="text" id="text">
<input type="button" value="See Code" onclick="myFunction();">
<input type="text" id="code" name="code">
<script>
function myFunction() {
var str = parseInt(document.getElementById("text").value);
var res = str.replace("ftpadress", "htmladress");
var code = str.concat("<img src='",res,"' width='100%'>");
document.getElementById("code").value = code;
}
</script>
I have an issue with the following code. I want to replace a string by another one, and generate an img code. However, I got an error message: str.replace is not a function
. Any idea why?
<input type="text" id="text">
<input type="button" value="See Code" onclick="myFunction();">
<input type="text" id="code" name="code">
<script>
function myFunction() {
var str = parseInt(document.getElementById("text").value);
var res = str.replace("ftpadress", "htmladress");
var code = str.concat("<img src='",res,"' width='100%'>");
document.getElementById("code").value = code;
}
</script>
Share
Improve this question
asked Feb 8, 2017 at 1:11
FloFlo
2091 gold badge4 silver badges13 bronze badges
3
-
3
str
is result ofparseInt
, so ,it's anumber
..replace()
is a String method. – luiscrjr Commented Feb 8, 2017 at 1:13 - Possible duplicate of Why do I get an error message that .replace is not a function? – Sebastian Simon Commented Feb 8, 2017 at 1:22
-
Why are you calling
parseInt()
on a value that might contain the string"ftpadress"
? – nnnnnn Commented Feb 8, 2017 at 2:14
3 Answers
Reset to default 7As @mrlew pointed out,
str
is result of parseInt
and therefore, it's a number. replace()
is a string method
, so it will not work on a number
.
If I understood correctly, you would like to replace a string, retrieve a code and then generate an image tag with the new string and code.
I'd go with...
<input type="text" id="text" />
<input type="button" value="See Code" onclick="myFunction();">
<input type="text" id="code" name="code">
<script>
function myFunction() {
//changed
var str = document.getElementById("text").value; //get text
var res = str.replace("ftpadress", "htmladress"); //replace
var code = parseInt(str).toString(); //get code and cast it back to a string
document.getElementById("code").value = code; //insert code
var withTag = code.concat("<img src='", res, "' width='100%'>"); //generate tag
}
</script>
parseInt returns an integer not a string so you can not use str.replace() , you need to cast it first
just add str = str.toString(); before using the replace function
Just remove the casting (parseInt function) and everything should work fine.
<input type="text" id="text">
<input type="button" value="See Code" onclick="myFunction();">
<input type="text" id="code" name="code">
<script>
function myFunction() {
//changed
var str = document.getElementById("text").value;
console.log(str);
var res = str.replace("ftpadress", "htmladress");
var code = str.concat("<img src='",res,"' width='100%'>");
document.getElementById("code").value = code;
}
</script>