Change variable when selecting another value in select in JS

2

How could I change the param value by what I selected in select? the param is in the link that I will be sending to another page. It would only be the id that I selected in the 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>
    
asked by anonymous 07.02.2016 / 21:47

1 answer

2

Use the attr function.

For example:

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

attr has two functions:

  • .attr('href') works as a getter and will get the value of the href attribute.
  • .attr('href', 'novo valor') functions as a setter and will change the href , in this case, to the selected select value.
  • You can test it by clicking here .

        
    07.02.2016 / 21:58