Change variable when selecting another value in select in JS

Asked

Viewed 1,207 times

2

How could I change the param value for what I selected in select? param is in the link I’m sending to another page. It would only be the id I selected in select.

var meu_select = $('#meu_select');
meu_select.change(function() {
    var valor = meu_select.val()
    location.href = '#?param=' + valor;
});
<form>
    <select id='meu_select'>
        <option value='1'>Valor 1</option>
        <option value='2'>Valor 2</option>
    </select>
    <a id="various3" class="various3" href="relatorio_gastos_rec.php?param">
        <img src="img/icones/editar.png" width="20" height="20" alt="Alterar" />
    </a>
</form>

1 answer

2


Use the function of attr.

For example:

$('#meu_select').change(function(){
      $('#various3').attr('href', 'relatorio_gastos_rec.php?param=' + this.value);
});

The attr has two functions:

  1. .attr('href') functions as a getter and will take the value of the attribute href.
  2. .attr('href', 'novo valor') functions as a Setter and will alter the href, in this case, for the selected select value.

You can test this by clicking here.

  • very good friend @Inkeliz thanks for the help worked perfectly imported the jquery library and worked.

Browser other questions tagged

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