Jignesh Gandhi Jignesh Gandhi - 2 years ago 88
HTML Question

how to replace repeated value in javascript

When i put "123" value into input field it will convert properly to letter "ABC", but when i type "112" it only convert first number like this "A1B" i need "112" to be convert into "AAB" can anyone help?

function char_convert() {

var chars = ["1","2","3","4","5","6","7","8","9","0"];
var codes = ["A","B","C","D","E","F","G","H","I","J"];

for(x=0; x<chars.length; x++){
for (i=0; i<arguments.length; i++){

arguments[i].value = arguments[i].value.replace(chars[x], codes[x]);


<div id="test">

<input type="text" id="txtBox" onchange="char_convert(this);"/>

Answer Source

You can split value and loop over it and replace necessary value.

var chars = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
var codes = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];

function char_convert(el) {

  el.value = el.value.split("").map(function(c) {
    return codes[chars.indexOf(c)];
<div id="test">
  <input type="text" id="txtBox" onchange="char_convert(this);" />

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download