1

I'm having a problem with my JavaScript code having moved it over to a content page in ASP.

It worked perfectly on a stand alone page.

<script type="text/javascript">
        function validateQ12(oSrc, args) {
            var rbtnList = document.getElementsByName('rbtn12');
            var noClicked = false;
            for (var i = 0; i < rbtnList.length; i++) {
                if (rbtnList[i].checked) {
                    noClicked = rbtnList[i].value == "No";
                    break;
                }
            }
            if (noClicked) {
                args.IsValid = true;                   
            }
            else
                args.IsValid = args.Value.trim().length > 0;                   
        }
    </script> 

I have gone through the html on both pages, on the one that worked had

<input id="rbtn12_1" type="radio" name="rbtn12" value="No" checked="checked" onclick="Q12iNo();">

The new one inside a content page has

<input id="MainContent_rbtn12_1" type="radio" name="ctl00$MainContent$rbtn12" value="No" checked="checked" onclick="Q12iNo();">

Clearly the name change must be causing the problem. How do I reference this new name in my javascript code?

saintsfanuk
  • 332
  • 1
  • 5
  • 19

2 Answers2

0

To get the rendered element name use the following:

<%= rbtn12.UniqueID %>

However in your case this may still not work as it looks like you need to loop through each radio button item.

See the following post:

Find out if radio button is checked with JQuery?

Which shows how to determine if a radio button collection has a selected value.

Community
  • 1
  • 1
Oliver
  • 8,794
  • 2
  • 40
  • 60
  • Maybe I misunderstood what you were trying to do, glad it helped! – Oliver Mar 15 '13 at 09:25
  • All it does is find out which one is checked so I can use that information to call my ASP Validation controls. After adding what you said it works exactly how it did on the stand alone page before I had to put it into a content page, so thanks. :) – saintsfanuk Mar 15 '13 at 09:26
0

Can you possibly add a className to your control and use getElementByClassName?

<input id="MainContent_rbtn12_1" type="radio" name="ctl00$MainContent$rbtn12" value="No" checked="checked" onclick="Q12iNo();" CssClass="Radio">

<script type="text/javascript">
        function validateQ12(oSrc, args) {
            var rbtnList = document.getElementsByClassName('Radio');
            var noClicked = false;
            for (var i = 0; i < rbtnList.length; i++) {
                if (rbtnList[i].checked) {
                    noClicked = rbtnList[i].value == "No";
                    break;
                }
            }
            if (noClicked) {
                args.IsValid = true;                   
            }
            else
                args.IsValid = args.Value.trim().length > 0;                   
        }
    </script> 
MaxDataSol
  • 358
  • 1
  • 2
  • 18