Search code examples
javascripthtmljqueryradio-button

Cant get values from different radio buttons groups


I want to know what am I missing. I have this radio inputs in a form:

<input type="radio" name="consulta" value="Ingreso">Ingreso
<input type="radio" name="consulta" value="Inversion">Inversi&oacute;n y Crecimiento<br<br>
<label style="font-weight:500;">Tipo:</label>
<input type="radio" name="tipo" value="Mensual">Mensual
<input type="radio" name="tipo" value="Diaria">Diaria

and to retrieve the selected value from each group i have this javascript code inside a function:

var tipoConsulta = $('input[@name="consulta"]:checked').val();
var frecuenciaConsulta = $('input[@name="tipo"]:checked').val();

The problem is that the "frecuenciaConsulta" variable takes the same value that the "tipoConsulta" variable, both variables get the selected value from the first group. I dont know what im doing wrong, what am I missing?


Solution

  • Square brackets are already an attribute select, no need for the @ symbol, try

    var tipoConsulta = $('input[name="consulta"]:checked').val();
    var frecuenciaConsulta = $('input[name="tipo"]:checked').val();
    

    EDIT

    Just out of interest, the syntax including the @ symbol for an attribute was valid up to jQuery 1.3

    reference link

    jQuery Attribute Selectors