Search javascript input value

Asked

Viewed 671 times

1

Opa,

I believe it should be simple, but, I did not get a perfect functioning.

I have a function that when clicked should check the value contained in an input text, the values are separated by a / and are whole. I need that when this function is executed, it sends an integer value and check that this value is contained in the input, if it is, you should remove it, if not, you should include it, I tried it like this:

var opcoes_selecionadas = document.getElementById("options_add_input").value;

if(opcoes_selecionadas.search("/"+opcao_id+"/")){
   alert('string encontrada');
}
else
{
   opcoes_selecionadas = opcoes_selecionadas + "/" + opcao_id;
   document.getElementById("options_add_input").value = opcoes_selecionadas;
}

But it’s not working.

2 answers

3

/**
  * @param String input - O id do input com o valor a ser adicionado/removido.
  * @param String output - O id do input com a lista atual de valores.
  *
  * @return void
  *
  **/
function doIt(input, output){

    // Guarda os textos.
    var i = document.getElementById(input).value;
    var o = document.getElementById(output).value;

    // Divide a lista em um array e busca o elemento em input.
    var a = o.split('/');
    var p = a.indexOf(i); 

    // Remove se o elemento existe. Adiciona do contrário.
    if(p > -1)
        a.splice(p, 1); 
    else
        a.push(i.toString());

    // Atualiza a lista com a união do array.
    document.getElementById(output).value = a.join('/');
}
<label>Entrada</label>
<input type="text" id="entrada" />
<label>Lista</label>
<input type="text" id="lista" />
<button onclick="doIt('entrada', 'lista');">Adicionar/Remover</button>

On your button just call the function with the ids of inputs:

<button onclick="doIt('opcao', 'options_add_input');"></button> 
  • Hey, buddy, sorry for the delay in responding, but when you enter the code, you’re adding normally, but not removing the var p is always returning -1, that is, even containing the value in the input is being added. var p = o.indexOf(i); , i.e., the indexef directly in the input var it returns the position of the value in the input, but, does not remove, just does not add. Could you give me a strength buddy?

  • Added functional snippet. Maybe your browser version does not support the indexOf; check out.

2

Hello, I don’t know if I understood it very well, but I’ll try to help, I did the following function:

document.getElementById('send').onclick = function() {
  checkNumber(300);
}

function checkNumber(number) {
  var options = document.getElementById("options_add_input").value;
  // Transforma o valor recebido do input em um array
  var arrOptions = options.split('/').map(function(item) {
        // Converte os valores em inteiros
		return parseInt(item, 10);
  });
  
  // Verifica se número recebido está no array, se não estiver é adicionado e se estiver é removido
  var index = arrOptions.indexOf(number);
  if (index == -1) {
  	arrOptions.push(300);
  } else {
  	arrOptions.splice(index, 1);
  }
  
  console.log(arrOptions);
}
<input type="text" id="options_add_input">
<button id="send">Send</button>

  • You could use var arrOptions = options.split('/').map(Number);, and compress a little bit more https://jsfiddle.net/Lp7L9vuk/, but I think the idea AP needs is this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.