Search code examples
javascripthtmljqueryradio-button

I want to use the text of radio inputs. How can i get it using jQuery or JavaScript


I want to use the text 5 and 6 shown below. I cant modify the html as it is auto-generated and there are many other radios with the same structure. How can i achieve this using jQuery or JavaScript and store them as variables.

<label class="radio">
    <label style="display: none;">
        <input type="radio" name="x_keeper1_price" id="x_keeper1_price_0" value="21"/>
    </label>
    5
</label>

<label class="radio">
    <label style="display: none;">
        <label style="display: none;">
            <input type="radio" name="x_Defd5_price" id="x_Defd5_price_0" value="28"/>
        </label>
    </label>
    6
</label>

Solution

  • If your text after the radio button is html code, this is a better solution:

    var text = $("#x_keeper1_price_0") //the radio button
               .closest("label.radio") //the label with class=radio)
               .html()                 //the html code
               .replace(/<label[\d\D]+<\/label>/, '');
               /* Removing the "second" label and its content.
                  That will get the text after the radio button. */
    alert(text);
    

    jsfiddle: http://jsfiddle.net/72KcV/3/