Syed Salman Raza Zaidi
Syed Salman Raza Zaidi

Reputation: 2192

ASP.net Radio button click in JQuery

I have asp.net radio button control in my application, I want to apply jquery click event on this control with class name. I have set CssClass property and made script with the class name,but its not working.When I inspect the HTML in firebug,its making the span with the CSSClass I have provided This is my aspx code

<asp:RadioButton ID="radioyes" CssClass="quest" GroupName="Ques" runat="server" Text="Yes" />
  <asp:RadioButton ID="radiono" CssClass="quest" GroupName="Ques" runat="server" Text="No" />

its rendering in browser as

<span class="quest"><input type="radio" value="quest2yes" name="ctl00$ContentBody$Wizard1$Q2" id="ctl00_ContentBody_Wizard1_radioyes"><label for="ctl00_ContentBody_Wizard1_radioyes">Yes</label></span>
    <span class="quest"><input type="radio" value="quest2no" name="ctl00$ContentBody$Wizard1$Q2" id="ctl00_ContentBody_Wizard1_radiono"><label for="ctl00_ContentBody_Wizard1_radiono">No</label></span>

When I am typing $(".quest").length in console,its showing 0 as well.

I am trying with this

$(".quest").click(function(){
});

I also tried with

 $('#<%=radioyes.ClientID%>').click(function () {
});

but still not working,its not even showing any error in console

Upvotes: 0

Views: 2822

Answers (2)

Deepak Ingole
Deepak Ingole

Reputation: 15732

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .click()

You are assigning event handler to element before it actually gets loaded in DOM.

So you must use event delegation as,

$("document").on("click", ".quest", function () {
    // rest code here
});

.on()

Upvotes: 1

Sachin
Sachin

Reputation: 40970

You might be using the code before the dom element actually loaded to the page.

Wrap your function inside $(document).ready(function ()

$(document).ready(function () {
    $(".quest").click(function () {
        alert('s');
    });
});

Upvotes: 4

Related Questions