3

EDIT: updated verbiage to reflect this is specifically a Radio Button choice field. The below script works when the choice is set to display in a drop down. I'd like to be able to do the equivalent when the display type is set to Radio Buttons.

I am trying to get the value of a choice field (set to display as radio buttons) using jQuery and SharePoint Online. I'm adding the script to a script editor web part on the edit form.

    <script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript" language="javascript">
       function PreSaveAction() {
          var myDesVal = $("select[title='Destruction Approval'] option:selected").val();
          alert(myDesVal);   
}
    </script>

The above returns undefined when a value is selected and I click the Save button. I would eventually like to make another field required if a particular Radio Button is selected.

3
  • You should update your question, stating that the element is Radio Button and not Drop Down. This will allow users to give correct solution.
    – Asad Refai
    Commented Dec 4, 2015 at 8:35
  • Is Destruction Approval the name of your field or one of the choice values?
    – Erin L
    Commented Dec 4, 2015 at 18:11
  • Destruction Approval is the field name with the values of Retain and Destroy. Ultimately, if Destruction Approval equals Retain, i want to force them to fill in a new Destruction Date.
    – Nate
    Commented Dec 4, 2015 at 18:13

5 Answers 5

8

Any modern browser does not need jQuery

document.querySelector("select[title='Destruction Approval'] option[selected]").innerHTML;

Will do the same as your code

But... radio-buttons are not a select with option elements...

They are input elements with the checked attribute set for the selected radio-button

document.querySelector("input[id^='Destruction Approval'][checked='checked']");

will return you the element or null to use in a condition

2

Change .val() to .text(). Also be sure to add </script> to the end.

If you're only looking to do validation with it, though, you could use the OOB list validation to require a field if a certain value from another field is chosen.

4
  • Thanks, Kaylee! I've tried both .val() and .text(). The only difference i see with using .text() is that the alert is blank (no value) instead of Undefined. Also, the </script> tag is there, i just neglected to include it in my code example.
    – Nate
    Commented Dec 3, 2015 at 21:38
  • Is it a drop down menu? Or radio buttons? Commented Dec 3, 2015 at 21:41
  • 2
    Radio buttons with two possible values.
    – Nate
    Commented Dec 3, 2015 at 21:47
  • When i change the field to a drop down instead of a radio button my code does work, so this is probably the route i'll go. I would be curious to see how you'd end up doing this with a radio button though, but not sure how you call that selector in a SharePoint form. Thanks for the help!
    – Nate
    Commented Dec 4, 2015 at 15:48
1

UPDATE: I ended up changing the column type to a drop down instead of radio buttons.

That said, I did recently have another project which required me to capture the value of a radio button. In looking at the code in Chrome developer tools, it appears as though SharePoint doesn't give a title to radio button fields as it does with other field types, but it does give a unique name viewable on each radio button which I was able to use to capture the value. I was able to return the value of the radio option selected with the below jQuery.

    var category = $("input[name='ctl00$ctl40$g_b619d8cf_c1d2_48d9_a627_48d5ea3f4e3f$ff181$ctl00$RadioButtons']:checked").val();
0

You don't need a script to accomplish this. Go to List Settings >> Validation Settings, and enter a formula that must be true and define a User Message that the user will see if the formula is false.

=OR([Destruction Approval]="Destroy",AND([Destruction Approval]="Retain",NOT(ISBLANK([Destruction Date]))))`
2
  • Thanks, Erin! This does what i need it to do. The reason i didn't go the validation settings route initially is because i also have to do this sort of thing in SharePoint 2007 as well and was hoping to find something reusable across both platforms. This definitely accomplishes what i need in this particular instance! I would still be interested to see how one might be able to grab that value when using jQuery or JavaScript.
    – Nate
    Commented Dec 4, 2015 at 19:38
  • Cool. Please accept this as the answer, but I'll come back to give you some JQuery later this afternoon.
    – Erin L
    Commented Dec 4, 2015 at 20:22
0

Can you try this code?

if($("td[id='tdID'] input:radio[name*='ff4']:checked +  label").text()   != 'Yes'){
// do something

}

*tdID is the id of the TD tag which has radio button control and 'ff4' is the ID of the SharePoint control.

Hope the below detailed post helps you in accessing the radio Buttons using JQuery in SharePoint. (Note: this helps when you have your List form customized in SPD)

Accessing Radio Buttons using JQuery in SharePoint Online

1
  • Hey Maruthu, that does help! My above code gets me the SharePoint value, which is usually something like ct101. The addition of the + label").text() gets me the text value/label which is a little more meaningful than the SharePoint value, which is useful but not very meaningful (if that makes sense).
    – Nate
    Commented Oct 25, 2016 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.