2

On click of checkbox, I need to retrieve immediate parent span's class value:

The checkbox column is defined in an ItemTemplate as:

 <asp:CheckBox CssClass='<%# Eval("ID") %>'  Checked='<%# Eval("IsSelected") %>' 
                                        Text="" runat="server"  onclick="CartItemCheckClicked()" />

The JS function is defined as:

function CartItemCheckClicked() {
        alert($(this).parent().attr('class')); //Undefined
        //alert($(this).attr('id')); //Undefined
    }

Ouput Sample HTML

<span class="283"><input type="checkbox" onclick="CartItemCheckClicked();" checked="checked" name="grvShoppingCart$ctl02$ctl00" id="grvShoppingCart_ctl00_0"></span>

But the result is always 'undefined'. How do I access the checkbox or parent span?

2
  • 1
    Is everything else resolving? Are you getting what you expect when you do alert($(this).attr('class'))? I'm guessing that this is not the this you're looking for. Commented Mar 3, 2011 at 13:19
  • yea.. when debugged in firefox 'this' = Window webpage.aspx. Commented Mar 3, 2011 at 13:21

1 Answer 1

3

Just pass checkbox to click function:

onclick="CartItemCheckClicked(this);"

And in js file than:

function CartItemCheckClicked(chk) {
        alert($(chk).parent().attr('class')); 
    }
Sign up to request clarification or add additional context in comments.

1 Comment

also since your using jQuery you can use it to bind the click event intead, then you dont have worry about passing the arg at all. i.e. $('input').click(function(){ alert($(chk).parent().attr('class')); })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.