How to take a result, look for some of the inconvenient words of an array and change it by another word from another array according to the order?

0

I have this input:

<form action="">
            Primer Apellido <br>
            <input type="text" name="apellidos" id="paterno" /     onkeyup="mayus(this);" onkeypress="Genera()">
            <br>
        </form>
      

I want that when I write for example "COLA" I change it to "COLX" and that this happens with all the other words that are in this array:

var inconvenientes2 = [ 'BACA', 'LOCA', 'BUEY', 'COLA' ];

And they should be changed by the words in this array according to the order:

var inconvenientes2 = [ 'BACX', 'LOCX', 'BUEX', 'COLX' ];
    
asked by A.Rafa 17.12.2017 в 03:11
source

2 answers

0

Good, you could use the indexOf function which returns the index if it finds it and -1 but, once the word is found, what you do is change the value of the input with the second array through the index.

var inconvenientes1 = [ 'BACA', 'LOCA', 'BUEY', 'COLA' ];
var inconvenientes2 = [ 'BACX', 'LOCX', 'BUEX', 'COLX' ];

function genera(self){
  const index = inconvenientes1.indexOf(self.value);
  if(index !== -1){
     self.value = inconvenientes2[index];
  }
}

function mayus(self){
  self.value = self.value.toUpperCase();
}
<input type="text" name="apellidos" id="paterno" oninput="mayus(this)" onkeyup="genera(this)">
    
answered by 17.12.2017 / 13:47
source
0

Good friend you have to get the value of input after doing a forEach to iterate the array inconvenientes1 and compare if what the user types matches some value in the array, if it matches we get the index and assign a new value to input with inconvenientes2[index]

I hope I help you.

Functional example

  

Note : the javascript library is used for the example jquery can be done without problem also with javascript vanilla but for speed use jquery.

var inconvenientes1 = [ 'BACA', 'LOCA', 'BUEY', 'COLA' ];
var inconvenientes2 = [ 'BACX', 'LOCX', 'BUEX', 'COLX' ];
$("#paterno").keyup(function(){
   var valor = $(this).val()
  inconvenientes1.forEach(function(item,index){
    if(item == valor.toUpperCase()){
    $("#paterno").val(inconvenientes2[index])
      console.log(inconvenientes2[index])
    }
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="">Primer apellido</label>
  <input id="paterno" type="text" name="" value="">
    
answered by 17.12.2017 в 04:25