Getting SharePoint radio button value from the DOM

I was stumped the other day with something that I thought would be easy.  I had a form and on the form a radio button with three options.  All I wanted to do was grab the value and I was having a hard time doing so.  I was able use the code below to do so.

var btn =  document.querySelector("input[name^='ctl00$ctl40$g']:checked").value

Note: the value returned is not the actual value you want but the value generated by SharePoint such as ‘ct100’, ‘ct101’, etc.

What you have to do is associate each of the generated values with your actual values and use a condition to test and take the appropriate action.  For example, if ‘ct100’ represent ‘Vanila’, you could do something such as the below.

    if (btn=='Vanila'){
      /*Do Something*/

Hope this was helpful. If you have another method, please share.

Thanks and happy coding! 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: