Time for action – animating between colors

In this example, we'll use color animations to show that a form field has been left empty.

  1. In a fresh copy of the template file, use the following elements in the <body> of the page:
    <input><button id="search">Search</button>
  2. To invoke the color changes when the <button> is clicked, we can use the following JavaScript in the empty function near the bottom of the document:
    $("#search").click(function (e) {
      e.preventDefault();
    
      var input = $(this).prev();
    
      if (input.val() == "") {
        input.animate({
          backgroundColor: "#f78080",
          borderTopColor: "#a72b2e",
          borderRightColor: "#a72b2e",
          borderBottomColor: "#a72b2e",
          borderLeftColor: "#a72b2e"
      }, 1200);
      };
    });
  3. Save this page as color-animations.html. We literally only ...

Get jQuery 1.4 Animation Techniques Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.