#Select an element in the DOM
$("#id"), $("<element>") or $(".class")
#Append to element
$("#foo").append("<div>hello world</div>")
Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts
Thursday, March 14, 2013
Wednesday, March 13, 2013
Basics If-Then Syntax for JS/JQuery
# standard syntax on JS
$(document).ready(function(){
var n = $("#example div").length;
if (n < 2) {
$("body").css("background", "green");
}
else {
$("body").css("background", "orange");
}
});
# this is the shorthand version
var n = $("#example div").length;
$("body").css("background", (n < 2) ? "green" : "orange");
# Checking if selector exists. When you use a selector, jQuery will always return an object. Therefore the if statement will always be true and never be false. So change it to this:
if ($("#mydiv").length > 0) {
// do something here
}
Monday, March 11, 2013
jQuery CDN and CSS Linking
# CDN - place after body tag
<script src="//ajax.googleapis.com/ajax/libs/webfont/1.1.2/webfont.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
# CSS Linking
<link href="styles.css" rel="stylesheet" type="text/css">
Saturday, March 9, 2013
Image Preloader
<script type="text/javascript">
$.preloadImages = function()
{
for (var i = 0; i<arguments.length; i++)
{
$("<img />").attr("src", arguments[i]);
}
}
$(document).ready(function()
{
$.preloadImages("hoverimage1.jpg","hoverimage2.jpg");
});
</script>
Check if element exists
if ($('#myElement').length > 0) {
// it exists
}
Chaining Events
$('#code1').css(
{
"background-color" : "white",
"color" : "#grey"
}
)
.slideUp(1000).slideDown(1000)
.animate({ width: '80%' , fontSize: "20px" } , 1800);
Animate Scrolling on Page
$("#id").click(function() {
$("html, body").animate({ scrollTop: $('#title').offset().top }, 1000);
});
Tuesday, March 5, 2013
Passing Events to jQuery Function
$("document").ready(function( ) {
#bind events with chaining
$('#id').bind('mousemove', findMouseCoords)
.click(event, getEvent);
});
function findMouseCoords( event )
{
$("#first-id").html(event.screenX);
$("#second-id").html(event.screenY);
}
function getEvent( event )
{
$("#third-id").html(event.target.nodeName);
$("#fourth-id").html(event.timeStamp);
}
#bind events with chaining
$('#id').bind('mousemove', findMouseCoords)
.click(event, getEvent);
});
function findMouseCoords( event )
{
$("#first-id").html(event.screenX);
$("#second-id").html(event.screenY);
}
function getEvent( event )
{
$("#third-id").html(event.target.nodeName);
$("#fourth-id").html(event.timeStamp);
}
Binding Events with jQuery
$("document").ready ( function( ) {
# binding events (eventname, function-name)
$('#id').bind('blur', myBlurEvent)
.bind('focus', myFocusEvent);
});
function myBlurEvent( )
{
$("#second").html("some comment");
}
# binding events (eventname, function-name)
$('#id').bind('blur', myBlurEvent)
.bind('focus', myFocusEvent);
});
function myBlurEvent( )
{
$("#second").html("some comment");
}
Monday, March 4, 2013
Multiple vs Single Method Calls
$("#foo").css("background-color", "red");
$("#foo").css("font-weight", "bold");
// becomes:
$("#foo").css({
"background-color": "red",
"font-weight": "bold"
});
$("#foo").css("font-weight", "bold");
// becomes:
$("#foo").css({
"background-color": "red",
"font-weight": "bold"
});
Document Ready Method
$(document).ready(function() {
// everything in here will be run when the DOM is
// ready to be interacted with
$(".bar").css("color", "blue").appendTo("#baz");
});
// everything in here will be run when the DOM is
// ready to be interacted with
$(".bar").css("color", "blue").appendTo("#baz");
});
Subscribe to:
Posts (Atom)