I am working with javascript and I have created a jsfiddle.
I have two textfields: one contains user input and the other one contains result. My problem is when I enter the same word twice then only one of them gets replaced.
For example, if I enter "going,going" in the first text field then the result is "GNG,going" - instead of "GNG,GNG". What am I doing wrong in my code?
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Scrolling DIV demo on iPhone / iPod Touch / Android / iPad</title>
</head>
<body>
<input type="text" id="first_text" value="going, used to, to do fast, as soon as possible"/>
<input type="text" id="second_text"/>
<input type="button" onclick="replaceText()" value="Click!"/>
</body>
</html>
Javascript:
var replaceText = function () {
var inputval = document.getElementById('first_text').value;
var arr = {
"going": "GNG",
"used to": "UD",
"as soon as possible": "ASAP",
"to do fast": "tdf"
}
for(var key in arr) {
if (typeof (arr[key]) !== "undefined") inputval = inputval.replace(key, arr[key])
}
document.getElementById("second_text").value = inputval;
}
\\before the characters.