Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attching JS to radio buttton group 1

Status
Not open for further replies.

mais123

Programmer
Dec 26, 2007
36
US
Hi, I have radio button group with 2 buttons, Yes and No. Few things:
1) How do I attach JS event to fire when user clicks Yes
2) In JS code, how can I check the value of the radio button group, basically if Yes or No is selected?

Thanks!!!
 
I'd ask in the javascript forum as both questions are specific to javascript and not really an ASP.NET issue (unless of course you want to then implement from the server side, in which case fell free to come back with what you want to implement).

If you do post in the js forum, make sure you post all of the relevant HTML that the browser sees (not the aspx page).


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
It can be accomplished by using the OnPreRender event of a radiobuttonlist (if that is what you are using)


ASPX
Code:
<asp:RadioButtonList ID="rbRootCause" runat="server" RepeatDirection="Horizontal"
    RepeatLayout="flow" [b]OnPreRender="setNoOption"[/b]>
    <asp:ListItem Value="1" Text="Yes"></asp:ListItem>
    <asp:ListItem Value="6" Text="No"></asp:ListItem>
</asp:RadioButtonList>
<div id="divWhyNot" style="display: none; padding-top: 5px;">
    Why?
    <asp:TextBox ID="txtWhyNot" runat="server" Width="500" /><br />
    <asp:Label ID="lblWhoSaid" runat="server" Style="font-size: 7pt; margin-left: 35px;" />
</div>
Code Behind
Code:
    public void setNoOption(object sender, EventArgs e)
    {
        string selVal = rbRootCause.SelectedIndex.ToString();
        foreach (ListItem li in rbRootCause.Items)
        {
            if (li.Text == "No")
            { li.Attributes["onclick"] = "showWhyNot(" + rbRootCause.ClientID + ",'block');"; }
            else
            { li.Attributes["onclick"] = "showWhyNot(" + rbRootCause.ClientID + ",'none');"; }
        }
    }
Javascript
Code:
function showWhyNot(clicked, show)
{
    var dynCtrl = clicked.id.substring(0,clicked.id.lastIndexOf("_")+1);
    divWhyNot.style.display=show;
    if (show == "block")
        document.getElementById(dynCtrl+"txtWhyNot").focus();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top