jQuery: find child and bind to event
I'm trying to bind to the change
event of what is rendered as a span
by ASP.NET from an asp:Label
. Obviously the container prefixes these controls with something such as ctl00_
, but I don't want to have my script dependent on identifiers that aren't necessarily known.
The span
is updated via the AjaxControlToolkit
PasswordStrength
control which uses the ID the an ASP.NET label to display its help messages base on an input control it is validating.
On looking into it a bit, it seems what I want is possible as the control has its own div
container - I found resources here, here and here that indicate I can essentially look-up the child with a selector, the SO answer looked absolutely promising so I came up with the following which alters only slightly:
<div id="PasswordHelpContainer" class="passwordFailureNotification">
<asp:Label runat="server" I开发者_开发问答D="PasswordHelpLabel"/>
</div>
$(document).ready(function () {
$('#PasswordHelpContainer').children('span:first').change(function () {
//
});
});
Unfortunately there was no success in my efforts; the event just doesn't fire.
What am I doing wrong here?
And am I mixing Javascript
and jQuery
together?
I know the second question might sound paradoxical to some but I'll freely elaborate if needs be.
From http://api.jquery.com/change/
The change
event is sent to an element when its value changes. This event is limited to <input>
elements, <textarea>
boxes and <select>
elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.
So your event will never get raised, since label is rendered as <span>
.
Ok, from what you said jQuery does not fire a change event for that change on itself. You either have to trigger
the event on the element yourself, or run the event handler from a different logic (probably ASP.NET provides something, I have no idea).
As far as jQuery is concerned, I am very sure a change event will not be triggered for what you described.
Also, you should've marked your edits with a "Edit" marker or something, it was very hard finding what change you made to the question.
精彩评论