The following piece of code can be used to set a timer using jQuery: $(document).ready(function() { window.setTimeout(function() { // some code }, 500); });
Category: jQuery
Check if an Element Is Visible
To check whether an element is visible, you can use the following piece of code: if($(element).is(“:visible”) == “true”) { //The DOM element is visible } else { //The DOM element is invisible }
Check if an Element Exists
To check whether an element exists, you can write the following code: if ($(“#someElement”).length) { //The DOM element exists } else { //The DOM element doesn’t exist }
Detect Current Mouse Coordinates
Now, suppose you need to detect the current mouse coordinates in the web browser in use. To do this, you can write the following piece of code: $(document).ready(function() { $().mousemove(function(e) { $(‘# MouseCoordinates ‘).html(“X Axis Position = ” + e.pageX + ” and Y Axis Position = ” + e.pageY); }); });
Change Element’s CSS Property
$(“#DivID”).css(“background-color”, “#000”); $(“#DivID”).css(“border”, “solid 2px #ff0000”);
Get & Set
Get & Set Textbox Values: //— GET & SET TEXTBOX VALUE —// //— CAN ALSO BE USED ON ANY OTHER ELEMENT THAT HAS A VALUE PROPERTY —// // get the value of a textbox var TextboxValue = $(“#TextboxID”).val(); // set the value of a textbox $(“#TextboxID”).val(“New Textbox Value Here”); Get & Set Element’s HTML: //— […]
Add & Remove CSS Classes
// add css class $(“#DivID”).addClass(“newclassname”); // remove css class $(“#DivID”).removeClass(“classname”); // add & remove class together $(“#DivID”).removeClass(“classname”).addClass(“newclassname”); // add & remove multiple classes $(“#DivID”).removeClass(“classname classname2”).addClass(“newclassname newclassname2”);
Animate Functions
//— ANIMATE (EXAMPLE USES OPACITY, BUT CAN USE ANY CSS PROPERTY. —// //— NOTE SOME MY REQUIRE THE USE OF A PLUGIN SUCH AS JQUERY COLOR ANIMATION PLUGIN. —// $(“#DivID”).animate({ opacity: 0.25 }, 1000); // do something when animation complete $(“#DivID”).animate({ opacity: 0.25 }, 1000, function () { alert(“Opacity Animation Complete”); });
Slide Functions
// toggle slide up and down $(“#DivID”).slideToggle(1000); // do something when animation complete $(“#DivID”).slideToggle(1000, function () { alert(“Slide Toggle Complete”); }); // slide up $(“#DivID”).slideUp(1000); // do something when animation is complete $(“#DivID”).slideUp(1000, function () { alert(“Slide Up Complete”); }); // slide down $(“#DivID”).slideDown(1000); // do something when animation is complete $(“#DivID”).slideDown(1000, function () { […]
Fade Functions
// fade in $(“#DivID”).fadeIn(1000); // do something when animation complete $(“#DivID”).fadeIn(1000, function () { alert(“Fade In Complete”); }); // fade out $(“#DivID”).fadeOut(1000); // do something when animation is complete $(“#DivID”).fadeOut(1000, function () { alert(“Fade Out Complete”); }); // fade to (fades to specified opacity) $(“#DivID”).fadeTo(1000, 0.25); // do something when animation is complete $(“#DivID”).fadeTo(1000, 0.25, […]