The code below adds a value into a textbox:
$('#mainTxtWeight').val(textWeight);
But what I want know is how can I select a radio button depending on what a value is using jquery?
Below is the radio buttons:
<table id="replies">
<tr>
<th colspan="2">Replies</th>
</tr>
<tr>
<td>Number of Replies:</td>
<td align="left">
<div class="replytd">
<label>
<input type="radio" name="reply" id="singlebtn " value="single" class="replyBtn"
/>Single</label>
</div>
<div class="replytd">
<label>
<input type="radio" name="reply" id="multiplebtn" value="multiple" class="replyBtn"
/>Multiple</label>
</div>
</td>
</tr>
</table>
I have created an if statement below, I just need help being able to select “Single” radio button if Reply Type equals “Single”, or select “Multiple” radio button if reply Type equals “Multiple”.
Below is if statement:
if(reply == "Single") {
//select "Single" radio button
} else if(reply == "Multiple") {
//select "Multiple" radio button
}
I want the exact same thing to occur with this line of code as well which is again “Single” and “Multiple” radio buttons:
var $btnReply = "<input type='radio' name='reply[" + count + "]' value='single' class='replyBtnRow' /> Single<br /><input type='radio' name='reply[" + count + "]' value='multiple' class='replyBtnRow' /> Multiple";
$replies.children().append($btnReply);
if($("input[name=reply]:checked").length) {
$replies.find('input[name="reply[' + count + ']"]').eq($("input[name=reply]:checked").val() == "single" ? 0 : 1).prop("checked", true);
}
UPDATE:
function addwindow(reply) {
if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
$('input[value="'+reply.toLowerCase()+'"]').attr('checked', true);
} else {
$(plusbutton_clicked).closest('tr').find('input[value="'+reply.toLowerCase()+'"]').attr('checked', true);
}
$.modal.close();
return false;
}
If you are trying to get the radio button which has a value of what’s in reply, which if I understand your question correctly, you can try this
assuming reply will always be ‘single’ or ‘multiple’