Obtain checked radio text

0

I have the following radio button that I filled with php, I need to bring the text "automatic" or "manual" if it is checked I have tried this without result:

$('input[name=tipo-caja]:checked').text();




<section>
                                <label>
                                    Tipo Caja
                                </label>
                                <div >
                                    <label class="radio">
                                        <input id="tipo-caja-manual" name="tipo-caja" type="radio" value="1" <?php if (isset($auto))if ($auto->caja=="1"){ ?> 
                                            checked=""            
                                          <?php } ?>>
                                            <i>
                                            </i>
                                            Manual
                                        </input>
                                    </label>
                                    <label class="radio">
                                        <input id="tipo-caja-auto" name="tipo-caja" type="radio" value="0" <?php if (isset($auto))if ($auto->caja=="0"){ ?> 
                                            checked=""            
                                          <?php } ?>>
                                            <i>
                                            </i>
                                            Automatica
                                        </input>
                                    </label>
                                </div>
                            </section>
    
asked by Javier Antonio Aguayo Aguilar 15.06.2017 в 18:14
source

2 answers

1

What I recommend is to handle values or attributes of type value in your radio button, and based on the value you get val() you process it with if/else or switch as it suits you, because the input do not have text as such that you can get text() then you would have to throw yourself an impractical selector to simulate what you want. I leave this example where I generate what you are looking for in a simple way but something different from your initial proposal. I hope it serves you.

$('input[name="tipo-caja"]').on('click', function(){
	console.log($(this).val());
  if ($(this).val() === "1"){
  	alert("Manual")
  }
  else {
  	alert("Automático")
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type ='radio' name ='tipo-caja' value='1'/>Manual
<input type ='radio' name ='tipo-caja' value='2'/>Automático
    
answered by 15.06.2017 / 18:23
source
0

It would not be easier to put the value you want in the radio value like this:

$('input[name="tipo-caja"]').on('click', function(){
    console.log($(this).val());
})

<input type ='radio' name ='tipo-caja' value='Manual'/>Manual
<input type ='radio' name ='tipo-caja' value='Automático'/>Automático
    
answered by 15.06.2017 в 18:25