Abdul Rahman Abdul Rahman - 27 days ago 6
Javascript Question

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

I have the following radio buttons within a PHP loop:

<form>
<table class="table table-bordered optionsChk">
<tr>
<td colspan="2"> Select Bench for case </td>
</tr>
<?php foreach($db->getRecordSet($sql) as $bench){ ;?>
<tr>
<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>
<td class="w95"> <label for="b_<?php echo($bench['sno']); ?>"><?php echo($bench['lbl']); ?></label> </td>
</tr>
<?php
}
?>


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();
$("#benchSno").val(benchSno);
$("#obj_lst").html("Selected Bench Label: "+$("input[type=radio][name=b_sno]:checked").text());
} catch(e) {
alert(e.message);
}
}


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

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

Try below code :

$('input[type="radio"]:checked').attr('alt');