trigger event from a child but have the parent listen fro it in Jquery
Is it possible to have a child trigger a custom event called CHANGED when it is selected but have its parent listen for that event. Is it possible through event bubbling up to the parent
I have a plugin from someone that is called like so. (#selector).dropdown();
Inside the dropdown it recreates a select list.
<select id="selectBox">
<option value="0">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
Now when one of the li selectors is clicked I want to trigger an event from it like below but have the parent listen for it. Parent meaning the slectBox element.
// INSIDE PLUGIN
$("li", ul).click(function () {
$(this).trigger('CHANGED');
});
//IN MY PAGE CODE
$(function(){
('#selectBox').dropdown();
('#selectBox').bind('CHANGED', update);
f开发者_运维知识库unction update(evt){
alert(evt.target);
}
});
So, I must be missing some context here, but if you can create an event, you know the element names, and you know when you want it performed, why not just add another event handler normally?
In your case:
$(function(){
$('#selectBox').bind('change', update);
function update(evt){
alert(evt.target);
}
});
http://jsfiddle.net/qruKH/
That is what the delegate function is for:
<script type="text/javascript">
$(document).ready(DocReady);
function DocReady()
{
$("#selectBox").delegate("option", "click", SelectClicked);
}
function SelectClicked()
{
alert("You clicked: " + this.text);
}
精彩评论