Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PHP jQuery Cookbook

You're reading from   PHP jQuery Cookbook jQuery and PHP are the dynamic duo that will allow you to build powerful web applications. This Cookbook is the easy way in with over 60 recipes covering everything from the basics to creating plugins and integrating databases.

Arrow left icon
Product type Paperback
Published in Dec 2010
Publisher Packt
ISBN-13 9781849512749
Length 332 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Vijay Joshi Vijay Joshi
Author Profile Icon Vijay Joshi
Vijay Joshi
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

PHP jQuery Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Handling Events with jQuery FREE CHAPTER 2. Combining PHP and jQuery 3. Working with XML Documents 4. Working with JSON 5. Working with Forms 6. Adding Visual Effects to Forms 7. Creating Cool Navigation Menus 8. Data Binding with PHP and jQuery 9. Enhancing your Site with PHP and jQuery Firebug Index

Adding events to elements that will be created later


The .bind() method attaches events to only those elements that exist on a page. If any new elements are created that match the criteria for the .bind() method, they will not have any event handlers.

How to do it...

  1. Create a new file in the chapter1 directory and name it as live.html.

  2. Write the HTML, which creates a button and a DIV on the page and styles them a bit.

    <html>
      <head>
        <title>Attaching events elements </title>
        <style type="text/css">
        div { border: 1px solid black;cursor:pointer;width:200px;margin:10px; }
        </style>
      </head>
      <body>
        <input type="button" id="button" value="Create New Element"/>
        
        <div class="future">Already on page</div>
      </body>
    </html>
  3. Time to spice things up with jQuery. Attach a click event to the button. This button will create the new DIV elements and will insert them into the page. Now attach a click event handler to the DIV using the live() method. Clicking on the DIV will change its CSS and HTML.

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function ()
      {
        $('#button').click(function()
        {
          $('body').append('<div class="future">I am a new div</div>');
        });
        
        $('div').live('click', function()
        {
          $(this).css({'color':'red','font-weight':'bold'}).html('You clicked me');
          });
        });
    </script>
  4. Run the live.html file and click on the DIV. You will see that its HTML and CSS has changed. Now click on the Create New Element button a few times to create some DIV elements. Clicking on any of these DIV elements will change their appearances. A typical screenshot after a few clicks will look similar to the following:

How it works...

The input button creates the new DIV elements and appends them to the body of a document. The secret lies in the next function. We have used jQuery's live() method to attach an event on click of a DIV element. live() behaves exactly like bind() for attaching events with only one major difference. Where bind() can add events to only existing elements on a page, live() remembers the attached event for that selector and applies it to matching elements even if they are created later and then inserted into a page.

Therefore, all new DIV elements that are created as a result of clicking on the Create New Element button also respond to the click event handler.

Removing event handlers with die()

The die() method is similar to the unbind() method. It is used to remove event handlers that were attached using the live() method. Similar to unbind(), die() also has two variations.

If it is called with no parameters, all event handlers will be removed. Another variation accepts an event type name that will remove that particular event:

$(element).die();

The following is the code for other variations that will remove only the specified event handler.

$(element).die('click');

If an element has more than one event handler attached to it, the above code will remove only the click event handler and will leave the others intact.

See also

  • Binding and unbinding elements provides basic information about adding and removing events from elements.

You have been reading a chapter from
PHP jQuery Cookbook
Published in: Dec 2010
Publisher: Packt
ISBN-13: 9781849512749
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image