I couldn't find anything obvious for accessing SharePoint Radio Buttons so chucked this together, hopefully useful for someone else :)
Note: The below code/solution is applicable to accessing radio button in a custom SharePoint List forms. To know how to create a custom form in SharePoint Online, read this article
Accessing Radio Button control in SharePoint is little tricky than accessing the radio buttons in HTML. The reason being, radio buttons are rendered as a table instead of a single control as shown below (radio button and it's preview in developer tools - Chrome):
So, each radio button is rendered inside SPAN tag in a table. To access the normal SharePoint controls, we will use either ID or title property but for Radio Buttons it is little complicated.
1. The actual radio button control is in "input" tag
2. The values (Example here: Yes, No) are inside "label" tag
3. But each control (Yes & No) are represented by ID which is generated by SharePoint
4. If you keenly look at the ID, there is "ff41" which is the ID of the control generated randomly by SharePoint.
Now, open the designer, add an ID to the TD of the Radio button so that we can query it easily using JSOM as shown below:
1 2 3 4 | //Radio button change event $("td[id='tdApproved'] input:radio[name*='ff4']").change(function(){ // do something }); |
1 2 3 | //Clear the values of radio button - reset the values of radio button $("td[id='tdApproved'] input[name*='ff4']")[0].checked = false; $("td[id='tdApproved'] input[name*='ff4']")[1].checked = false; |
1 2 3 4 | //Checking the value of radio button if($("td[id='tdApproved'] input:radio[name*='ff4']:checked + label").text() != 'Yes'){ // do something } |
1 2 | //Focus Radio button 1st element in control $("td[id='tdApproved'] input:radio[name*='ff4']:eq(0)").focus(); |
1 2 3 4 5 6 7 8 9 | //Align all Radio Buttons in the page horizontally function HorizontalAlignChoices() { var objSpans = $(".ms-RadioText"); objSpans.each(function () { if ($(this).is("span")) { $(this).closest("tr").css({ "float": "left" }); } }); } |
*** "tdApproved" is the ID of the SharePoint Radio Button Control, "ff4" is the unique ID of the SharePoint Control in your List form. Change this ID according to your form to get the exact result.
Please share your valuable comments which will make me write more and also share this post using the below social buttons to others.
Happy Share(ing)Point!
Update: 21/06/2017
Read this article, to know how to access Checkbox in SharePoint using JQuery 😎