jQuery Selectors
$('#ex1DivButton').click(function () {
let colour = getRandomColour();
$('#demo1 div').css("background-color", colour);
});
$('#ex1ClassButton').click(function () {
let colour = getRandomColour();
$('.ex1DemoClass').css("background-color", colour);
});
$('#ex1IDButton').click(function () {
let colour = getRandomColour();
$('#ex1DemoID').css("background-color", colour);
});
Div
Div Class
Div Class ID
Selector Filtering
$('input:radio[name=radio-group]').change(function() {
$('#ex2Output').html($('input:radio[name="radio-group"]:checked').val());
});
Selected option is
Click Event
$('.ex3Click').click(function () {
$(this).css("background-color", getRandomColour());
});
Click me
or me
or me
Hover
$("#overlay").hide();
$(".hover").hover(function () {
$("#overlay").css($(this).offset());
$("#overlay").html($(this).css("background-color").toString()).show();
}, function () {
$("#overlay").hide();
});
| 1 | 2 | 3 |
| 4 | 5 | 6 |
Fade in/out
let isFaded = false;
$('#ex7button').click(function() {
$('#ex7img').fadeOut("slow");
if (isFaded)
$('#ex7img').fadeIn("slow");
else
$('#ex7img').fadeOut("slow");
isFaded = !isFaded;
});
Handling Mouse Input
$('#demo3').click(function() {
$('#lightbox').slideToggle(500);
});
Lightbox
Animation Speed
let ex9Animated = false;
$('#ex9 button').click(function () {
let x = ex9Animated ? '0px' : '700px';
$('#ex91').animate({left: x}, "fast");
$('#ex92').animate({left: x}, "slow");
$('#ex93').animate({left: x}, 1000);
ex9Animated = !ex9Animated;
});
Fast
Slow
1000ms
Animation Chaining
$('#ex10 button').click(function () {
let target = $('#ex10animate');
target.animate({height: '500px', opacity: '0.5'}, "slow")
.animate({height: '500px', width: '500px', opacity: '1'}, "slow")
.animate({height: '100px', opacity: '0.5'}, "slow")
.animate({width: '100px', opacity: '1'}, "slow");
});
Input fetching & element creation
$('#ex11 button').click(function () {
$('#ex11 table').append( // append table row
$('').append( // append table data
$('').text( // set the text of td
$('#ex11 input:text').val() // fetch input value from user
)
)
);
});