alenan2013 alenan2013 - 21 days ago 4
Javascript Question

How to select multiple elements by id using jQuery

I have two radio buttons:

<p>
<label for="input_1">Path to Excellent </label>
<input id="input_1" name="radio" type="radio" value="1" />
</p>
<p>
<label for="input_2">Award of Excellence</label>
<input id="input_2" name="radio" type="radio" value="2" />
</p>


I would like to get val() from one of them using their ids: "input_1" or "input_2" by jQuery, something like this:

$('input[#input_1,#input_2]:checked').val();


My aim is to get val() from one of them that is checked, by id. How can I do this?

Answer

You can check the starts of the id in this case:

$("input[id^='input_']:checked").val();