This is how the file script.js is referred to in the html file:
<script type="text/javascript" src="script.js"></script>
I note that </script> is needed since script is not a self-closing tag.
$(document).ready(...) ready means when the document is loaded, so what's inside the parentheses will be executed when it loads. This "ready" is like an "if" I guess but a function of the "document" JQuery object.
Inside the "ready" function we can put a set of instructions (like a function definition) like this
function{ ... }
$( "div").fadeOut(1000); means the div will disappear gradually, it will take 1000 msec to do so.
'div' also works instead of "div".
$('#id_name').fadeOut(1000);
$('div').slideDown('slow');
$('div').mouseenter(function(){ /*this is also like an if statement*/
$('div').hide();
}) /*when the mouse is over any div block, all the divs will hide.*/
$('div').mouseenter(function(){
$('div').fadeTo('slow',0.25);
}); /*0.25 is the opacity value; 1 is maximum*/
$('div').mouseleave(function(){
$('div').fadeTo('fast',1);
});
$(this).hover(function{...},function{...}); /*the first function is for when the mouse enters the object and the second is when the mouse is out of it.*/
I tested it and compared it to using the mouseenter(...) and mouseleave(...) both, and the result was the same.
$('div').click(function(){ ... };
$('div').fadeOut('slow');
$('div').fadeIn('slow');
var $div = $('div');
or maybe
var divis = $('div');
A class also has a JQuery object:
var $divTypeOne = $('.class_name');
or even an id:
var $divOne = $('#id_name');
or a compound selector
var $liAndP = $('li, p');
$(this) refers to the event-handler (which is e.g. the JQuery object that the mouse is over or on which the mouse is clicked)
One variable definition can be:
$("p").remove(); /*removes the element (and all its descendants, normally, I guess)*/
$("p").empty(); /*keeps the element, but empties it and removes its descendants*/
We want to add a class to an element; let's see an example
$('#text').addClass('class_name');
Notice that class_name is not preceded by '.' because the name is sufficient and we're not selecting an element.
$('#text').removeClass('class_name');
$('#text').toggleClass('highlighted'); /*adds the class if removed and removes the class if added*/
Any particular attribute can be changed, e.g.
$('div').height('200px'); /*we could've also written*/
$('div').css('height','200px');
$('div').css('border-radius','10px');
To set the value of an element (it's the first found element)
$(document).ready(function(){
$("p").html("jQuery magic in action!");
})
To get the value:
alert($("p").val()); /*it didn't show anything for some reason?!*/
It's working like this though:
var toAdd = $('input[name=checkListItem]').val();
This event handler on() can work on elements or selectors that might exist sometime after the webpage has loaded.
$(document).on('click', '.item', function(){
$('this').remove();
});
Another event handler is the dblclick()
$('div').dblclick(function(){ ... }
$('input').focus(function(){...}) /*when the HTML input selector gets focus, either by using the tab or by clicking into it, this event handle is triggered*/
$(document).keydown(function(){
$('div').animate({left: '+=10px'},500); })
This one is ok too.
$(document).on('keydown',$(document),function(){
$('div').animate({left:'+=10px'},500);
});
$(document).keydown(function(pressed_key){ /*this is an input to the function triggered by the event handler*/
/* 37 is the left key, then top, then right, then down.*/
/* pressed_key.which is the value of the pressed key.*/
$('div').animate({left: '+=10px'},500); })
We might use parseInt(pressed_key.which, 10) means to consider the number originally as decimal.
parseInt(pressed_key.which, 16) means to consider the number originally as hexadecimal and tranform it into decimal.
$('img').animate({top: "+=10px"}, 'fast'); /*this lets the image go down*/
$('img').animate({top: "-=10px"}, 'fast'); /*this lets the image go up*/
This one doesn't work unfortunately
$('img').animate({bottom: "+=10px"}, 'fast');
nor this one
$('img').animate({right: "+=10px"}, 'fast');
To have the explode effect of a div e.g., we have to include an extra script tag that is the JQueryUI library:
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
and that's the syntax:
$(this).effect('explode');
Another effect is the bounce(... , ...) :
$('div').effect('bounce', {times:3},500);
$('div').effect('slide');
$("#menu").accordion(...); /*where #menu is the id of a particular div structured as a menu*/
Even .accordion() with no argument is ok.
$('#car').draggable(); /*we can click on it and drag it*/
$('div').resizable(); /*it resized just from the left!?*/
$('ol').selectable(); /*maybe this works best with lists, this adds the clicked li into the class ui-selected*/
an alternative can be this one found here
ol .ui-selected { ... }
$('ol').sortable(); /*to rearrange the lists as we like*/
<script type="text/javascript" src="script.js"></script>
I note that </script> is needed since script is not a self-closing tag.
$(document).ready(...) ready means when the document is loaded, so what's inside the parentheses will be executed when it loads. This "ready" is like an "if" I guess but a function of the "document" JQuery object.
Inside the "ready" function we can put a set of instructions (like a function definition) like this
function{ ... }
$( "div").fadeOut(1000); means the div will disappear gradually, it will take 1000 msec to do so.
'div' also works instead of "div".
$('#id_name').fadeOut(1000);
$('div').slideDown('slow');
$('div').mouseenter(function(){ /*this is also like an if statement*/
$('div').hide();
}) /*when the mouse is over any div block, all the divs will hide.*/
$('div').mouseenter(function(){
$('div').fadeTo('slow',0.25);
}); /*0.25 is the opacity value; 1 is maximum*/
$('div').mouseleave(function(){
$('div').fadeTo('fast',1);
});
$(this).hover(function{...},function{...}); /*the first function is for when the mouse enters the object and the second is when the mouse is out of it.*/
I tested it and compared it to using the mouseenter(...) and mouseleave(...) both, and the result was the same.
$('div').click(function(){ ... };
$('div').fadeOut('slow');
$('div').fadeIn('slow');
var $div = $('div');
or maybe
var divis = $('div');
A class also has a JQuery object:
var $divTypeOne = $('.class_name');
or even an id:
var $divOne = $('#id_name');
or a compound selector
var $liAndP = $('li, p');
$(this) refers to the event-handler (which is e.g. the JQuery object that the mouse is over or on which the mouse is clicked)
One variable definition can be:
$p = $("<p>I'm a new paragraph!</p>");
$(".info").append("<p>I'm the new last child added to the class info!</p>");
$(".info").prepend("<p>I'm the new first child added to the class info!</p>");
$("<p>I'm the new first child added to the class info!</p>").prependTo(".info");
$("<p>I'm the new last child added to the class info!</p>").appendTo(".info");
$(".info").after("<p>I'm a child added after the class info!</p>");
This creates (adds) a new paragraph.
but
$(".info1").after($("p"));
moves the existing $("p") paragraph after class .info1
A side note:
for some reason this isn't working:
$(document).ready(function () {
$(".container:nth-child(2)").after("<p>sdfas</p>");
});
but this works:
$(document).ready(function () {
$(".container.first-child").after("<p>sdfas</p>");
});
It was in lesson 3/14 of Modifying HTML Elements.
$("p").remove(); /*removes the element (and all its descendants, normally, I guess)*/
$("p").empty(); /*keeps the element, but empties it and removes its descendants*/
We want to add a class to an element; let's see an example
$('#text').addClass('class_name');
Notice that class_name is not preceded by '.' because the name is sufficient and we're not selecting an element.
$('#text').removeClass('class_name');
$('#text').toggleClass('highlighted'); /*adds the class if removed and removes the class if added*/
Any particular attribute can be changed, e.g.
$('div').height('200px'); /*we could've also written*/
$('div').css('height','200px');
$('div').css('border-radius','10px');
To set the value of an element (it's the first found element)
$(document).ready(function(){
$("p").html("jQuery magic in action!");
})
To get the value:
alert($("p").val()); /*it didn't show anything for some reason?!*/
It's working like this though:
var toAdd = $('input[name=checkListItem]').val();
This event handler on() can work on elements or selectors that might exist sometime after the webpage has loaded.
$(document).on('event', 'selector', function() {
Do something!
});
e.g.$(document).on('click', '.item', function(){
$('this').remove();
});
Another event handler is the dblclick()
$('div').dblclick(function(){ ... }
$('input').focus(function(){...}) /*when the HTML input selector gets focus, either by using the tab or by clicking into it, this event handle is triggered*/
$(document).keydown(function(){
$('div').animate({left: '+=10px'},500); })
This one is ok too.
$(document).on('keydown',$(document),function(){
$('div').animate({left:'+=10px'},500);
});
$(document).keydown(function(pressed_key){ /*this is an input to the function triggered by the event handler*/
/* 37 is the left key, then top, then right, then down.*/
/* pressed_key.which is the value of the pressed key.*/
$('div').animate({left: '+=10px'},500); })
We might use parseInt(pressed_key.which, 10) means to consider the number originally as decimal.
parseInt(pressed_key.which, 16) means to consider the number originally as hexadecimal and tranform it into decimal.
$('img').animate({top: "+=10px"}, 'fast'); /*this lets the image go down*/
$('img').animate({top: "-=10px"}, 'fast'); /*this lets the image go up*/
This one doesn't work unfortunately
$('img').animate({bottom: "+=10px"}, 'fast');
nor this one
$('img').animate({right: "+=10px"}, 'fast');
To have the explode effect of a div e.g., we have to include an extra script tag that is the JQueryUI library:
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
and that's the syntax:
$(this).effect('explode');
Another effect is the bounce(... , ...) :
$('div').effect('bounce', {times:3},500);
$('div').effect('slide');
$("#menu").accordion(...); /*where #menu is the id of a particular div structured as a menu*/
Even .accordion() with no argument is ok.
$('#car').draggable(); /*we can click on it and drag it*/
$('div').resizable(); /*it resized just from the left!?*/
$('ol').selectable(); /*maybe this works best with lists, this adds the clicked li into the class ui-selected*/
an alternative can be this one found here
And in the stylesheet.css text file we can add whatever we like in this selector:$("li").bind('click', function(){ /*or $('li').click(function(){ */ // remove theui-selected class if it's there if($("li.ui-selected").length) $("li.ui-selected").removeClass('ui-selected'); // add theui-selected class to the clicked element $(this).addClass('ui-selected'); });
ol .ui-selected { ... }
$('ol').sortable(); /*to rearrange the lists as we like*/