-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery13-eventosPersonalizados.html
41 lines (41 loc) · 1.14 KB
/
jquery13-eventosPersonalizados.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Eventos personalizados</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).on('ready',function(){
// Como crear eventos personalizados
// Con el evento change conseguimos el mismo resultado
// Este ejemplo implementa ese comportamiento como si no existiera change
$("#miInput").on('cambio', function(ev,id){
var ele=$('#miInput');
//Esta función tiene dos parámetros: ev (evento) y id (id del elemento
//que lanzó el evento
console.log(id);
// Observa que utilizamos el método prop para leer y escribir
// los atributos del checkbox
if(ele.prop('checked'))
{
ele.prop('checked',false);
$('.cambiar').text('Encender');
}
else
{
ele.prop('checked',true);
$('.cambiar').text('Apagar');
}
});
$('button').on('click',function(){
$('#miInput').trigger('cambio',[$(this).attr('id')]);
});
});
</script>
</head>
<body>
<input type='checkbox' id='miInput' checked="checked">
<button class='cambiar' id="b1">Apagar</button>
<button class='cambuar' id="b2">Apagar</button>
</body>
</html>