Abdul Rahman Abdul Rahman -4 years ago 119
Javascript Question

Get Alt="" value from the selected radio button with jQuery

I have the following radio buttons within a PHP loop:

<table class="table table-bordered optionsChk">
<td colspan="2"> Select Bench for case </td>
<?php foreach($db->getRecordSet($sql) as $bench){ ;?>
<td class="w5">
<input type="radio" alt="<?php echo($bench['lbl']); ?>" value="<?php echo($bench['sno']); ?>" name="b_sno" id="b_<?php echo($bench['sno']); ?>">
<td class="w95"> <label for="b_<?php echo($bench['sno']); ?>"><?php echo($bench['lbl']); ?></label> </td>

I am displaying the values of the above with the following function:

function doneBenchSelection() {
try {
var benchSno = $("input[type=radio][name=b_sno]:checked").val();
$("#obj_lst").html("Selected Bench Label: "+$("input[type=radio][name=b_sno]:checked").text());
} catch(e) {

I want to get the
alt="some text"
text from the selected radio button:

$("#obj_lst").html("Selected Bench Label: "+$("input[type=radio][name=b_sno]:checked").text());

Is it the correct way? Or is this not the recommended way? Please help.

Answer Source

To get the attribute value of any element you can use attr method or you can also use .prop method

Try below code :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download