<!DOCTYPE html PUBLIC "- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>jQuery triggerHandler() Demo</title>
<style type="text/css">
p.field {
border: 1px solid #666666 ;
padding: 10px 10px 10px 10px ;
}
p.selected-field {
background-color: #F0F0F0 ;
}
</style>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
var arrData = [
{
checkbox: "chb1",
span: "Here is some sample text"
},
{
checkbox: "chb2",
span: "Here is some sample text"
},
{
checkbox: "chb3",
span: "Here is some sample text"
}
];
$( InitForm );
function InitForm(){
jForm = $( "form" );
$.each(
arrData,
function( intIndex, objValue ){
var jPara = $( "<p />" ).addClass( "field" );
var jCheckbox = $(
"<input type='checkbox' name='" +
objValue.checkbox +
"' />"
);
var jSpan = $(
"<span>" +
objValue.span +
"</span>"
);
jPara
.append( jCheckbox )
.append( " " )
.append( jSpan )
.appendTo( jForm )
;
}
);
jForm.find( ":checkbox" ).click(
function( objEvent ){
jParent = $( this ).parents( "p.field" );
if (this.checked){
jParent.addClass( "selected-field" );
} else {
jParent.removeClass( "selected-field" );
}
}
);
jForm.find( "span" ).click(
function( objEvent ){
var jThis = $( this );
var jCheckbox = jThis.prev( ":checkbox" );
jCheckbox[ 0 ].checked = !jCheckbox[ 0 ].checked;
jCheckbox.triggerHandler( "click" );
}
);
}
</script>
</head>
<body>
<h1>
jQuery triggerHandler() Demo
</h1>
<form>
</form>
</body>
</html>