none
SharePoint 2013- How to check using jquery/JavaScript, any option of radio button is selected or not without using ID or name? RRS feed

  • Question

  • Hi ,

    Below is html Code. How to check using jquery/JavaScript, any option of radio button is selected or not without using ID or name as control id and name will change in production(pl also refer attached image) and please consider there are two radio button in page with same value Yes,No but with different Label like for below one it's "Will you site contain ZNA records" and for second one is "Do you want team site"

    <tr>
    	<td width="190px" valign="top" class="ms-formlabel"><h3 class="ms-standardheader"><nobr>Will your site contain ZNA Records?<span class="ms-formvalidation"> *</span></nobr></h3></td>
    	<td width="400px" valign="top" class="ms-formbody"><span dir="none"><table cellpadding="0" cellspacing="1">
    					<tbody>
    					<tr>
    						<td><span class="ms-RadioText" title="Yes"><input id="ctl00_ctl44_g_c11829d7_aa76_49cc_862a_14f0a58d7c40_ff331_ctl00_ctl00" type="radio" name="ctl00$ctl44$g_c11829d7_aa76_49cc_862a_14f0a58d7c40$ff331$ctl00$RadioButtons" value="ctl00"><label for="ctl00_ctl44_g_c11829d7_aa76_49cc_862a_14f0a58d7c40_ff331_ctl00_ctl00">Yes</label></span></td>
    					</tr>
    					<tr>
    						<td><span class="ms-RadioText" title="No"><input id="ctl00_ctl44_g_c11829d7_aa76_49cc_862a_14f0a58d7c40_ff331_ctl00_ctl01" type="radio" name="ctl00$ctl44$g_c11829d7_aa76_49cc_862a_14f0a58d7c40$ff331$ctl00$RadioButtons" value="ctl01"><label for="ctl00_ctl44_g_c11829d7_aa76_49cc_862a_14f0a58d7c40_ff331_ctl00_ctl01">No</label></span></td>
    					</tr>
    				</tbody></table></span>
    	</td>
    </tr>

    


    Vishal Goyal




    • Edited by MS_21212 Sunday, April 23, 2017 9:31 AM
    Sunday, April 23, 2017 9:03 AM

Answers

  • Try below JQuery code if your label text not going to change...

    alert($("nobr:contains('Will your site contain ZNA Records?')").parent().parent().next().find("input[type='radio']").is(":checked")); // Return true if its selected or return false if its not selected

    Note : Don't forget to add JQuery file in your page...

    Sunday, April 23, 2017 10:45 AM
  • Hi,

    Dillibabub had provided the solution, just give a tip for parents function of jQuery.

    You could optimize the parent().parent() to parents(1)

    alert($("nobr:contains('Will your site contain ZNA Records?')").parents(1).next().find("input[type='radio']").is(":checked"));

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Proposed as answer by Abhijeet Thorat Tuesday, May 2, 2017 12:07 PM
    • Marked as answer by MS_21212 Monday, May 8, 2017 9:57 AM
    Monday, April 24, 2017 10:12 AM

All replies

  • Try below JQuery code if your label text not going to change...

    alert($("nobr:contains('Will your site contain ZNA Records?')").parent().parent().next().find("input[type='radio']").is(":checked")); // Return true if its selected or return false if its not selected

    Note : Don't forget to add JQuery file in your page...

    Sunday, April 23, 2017 10:45 AM
  • Hi,

    Dillibabub had provided the solution, just give a tip for parents function of jQuery.

    You could optimize the parent().parent() to parents(1)

    alert($("nobr:contains('Will your site contain ZNA Records?')").parents(1).next().find("input[type='radio']").is(":checked"));

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Proposed as answer by Abhijeet Thorat Tuesday, May 2, 2017 12:07 PM
    • Marked as answer by MS_21212 Monday, May 8, 2017 9:57 AM
    Monday, April 24, 2017 10:12 AM
  • Hi Vishal,

    Is this radio button's added by you or it's out of the box?

    If you are adding, any control in your .aspx page, you can provide the id/name attribute to that and with the help of JQuery selector you can select it:

                //There are 3 built-in attribute selectors for simple patterns:
    
    
                $('body,html').find('select[id^="foo"]').each(function() { 
                    //To do
                });
    
                //That selector matches all object that have an id attribute and it starts with foo (e.g. fooblah)
    
                    $('body,html').find('select[id$="foo"]').each(function() { 
                        //To do
                    });
    
                //That selector matches all spans that have an id attribute and it ends with foo (e.g. blahfoo).
                    $('body,html').find('select[id*="foo"]').each(function() { 
                    //To do
                });
    
                //That selector matches all spans that have an id attribute and it has foo somewhere within in it (e.g.  blahfooblah).


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog
    Please click "Mark As Answer" if this post solves your problem or "Vote As Helpful" if it was useful

    Monday, April 24, 2017 11:24 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can share your own solution here and mark it as answer if it had been solved, so it may help others who has similar requirement.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, May 2, 2017 8:58 AM