+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

Thread: jQuery: Events

  1. #1
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    jQuery: Events

    G'day everyone.

    This is the third sequence of my jQuery tutorials. The first one was all the selectors, the second was making a table stripey, now this one will be
    using events such as onclick, onkeyup etc. in jQuery. I said in my previous tutorial I would be showing you how to sort data easily in jQuery, but I
    decided to change my mind. The next tutorial will show you one of the ways you can sort the table data, using AJAX and MySQL. The one after that, it
    will be even MORE simple! We will be using a jQuery plugin.

    As I mentioned in the previous tutorials, you must have the jQuery.js file. This can be found in the first tutorial. Another thing you might need for
    this tutorial will be the previous source code from tutorial 2. If you have been following you might have created your own code, but here is mine if
    you didn't;
    Code:
    <!DOCTYPE html 
         
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >

    <
    html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <
    head>
            <
    title>Zebra Stripes</title>
            
            <
    style type="text/css">
                
    thead tr th {
                    
    background-color#93008E;
                
    }
                
                
    tr {
                    
    background-color#FFADFC;
                
    }
                
                
    tr.alt td {
                    
    background-colorwhite;
                }
                
                
    tr.over tdtr:hover td {
                    
    background-color#93008E;
                
    }
            </
    style>
            
            <
    script src="jQuery.js" type="text/javascript"></script>
            
            <script type="text/javascript">
                $(document).ready(function() {
                    $(".zebra tr:even").addClass("alt");
                });
            </script>
        </head>
        <body>
        
            <table width = "650" class = "zebra">
                <thead>
                    <tr>
                        <th>
                            Name
                        </th>
                        <th>
                            Rank
                        </th>
                        <th>
                            Post Count
                        </th>
                        <th>
                            Avatar Rating
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            Jordan
                        </td>
                        <td>
                            Administrator
                        </td>
                        <td>
                            12,447
                        </td>
                        <td>
                            8
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Xav
                        </td>
                        <td>
                            Code Slinger
                        </td>
                        <td>
                            12,146
                        </td>
                        <td>
                            7
                        </td>
                    </tr>
                    <tr>
                        <td>
                            TcM
                        </td>
                        <td>
                            Code Warrior
                        </td>
                        <td>
                            8,450
                        </td>
                        <td>
                            9
                        </td>
                    </tr>
                    <tr>
                        <td>
                            WingedPanther
                        </td>
                        <td>
                            Super Moderator
                        </td>
                        <td>
                            5,079
                        </td>
                        <td>
                            7
                        </td>
                    </tr>
                    <tr>
                        <td>
                            chili5
                        </td>
                        <td>
                            Code Warrior
                        </td>
                        <td>
                            4,431
                        </td>
                        <td>
                            9
                        </td>
                    </tr>
                    <tr>
                        <td>
                            marwex89
                        </td>
                        <td>
                            Guru
                        </td>
                        <td>
                            3,995
                        </td>
                        <td>
                            6
                        </td>
                    </tr>
                    <tr>
                        <td>
                            John
                        </td>
                        <td>
                            Co-Administrator
                        </td>
                        <td>
                            3,762
                        </td>
                        <td>
                            10
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Egz0N
                        </td>
                        <td>
                            Guru
                        </td>
                        <td>
                            3,674
                        </td>
                        <td>
                            9
                        </td>
                    </tr>
                    <tr>
                        <td>
                            amrosama
                        </td>
                        <td>
                            Code Warrior
                        </td>
                        <td>
                            3,283
                        </td>
                        <td>
                            7
                        </td>
                    </tr>
                    <tr>
                        <td>
                            v0id
                        </td>
                        <td>
                            Retired
                        </td>
                        <td>
                            2,697
                        </td>
                        <td>
                            5
                        </td>
                    </tr>
                    <tr>
                        <td>
                            MathXpert
                        </td>
                        <td>
                            Guru
                        </td>
                        <td>
                            2,263
                        </td>
                        <td>
                            8
                        </td>
                    </tr>
                    <tr>
                        <td>
                            mendim.
                        </td>
                        <td>
                            Guru
                        </td>
                        <td>
                            2,032
                        </td>
                        <td>
                            8
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Brandon W
                        </td>
                        <td>
                            Guru
                        </td>
                        <td>
                            2,004
                        </td>
                        <td>
                            10
                        </td>
                    </tr>
                    <tr>
                        <td>
                            MikeM
                        </td>
                        <td>
                            Guru
                        </td>
                        <td>
                            1,494
                        </td>
                        <td>
                            10
                        </td>
                    </tr>
                </tbody>
            </table>
            
        </body>
    </html> 
    What we will be doing in this tutorial, is adding a onhover effect to the elements with the class "over". As you can see there is no elements, but
    if you read the previous tutorial, I highly suggest you do, you will understand why there isn't an element, yet, with the class over. Also the hover
    effect already works because we added it the CSS, but what about the browsers that don't support :hover *cough*IE*cough*? jQuery can do this too.

    So let's get started;
    Here is some new jQuery code you must insert;
    Code:
    $(".zebra tr").mouseover(function() {
        $(
    this).addClass("over");
    }); 
    This must still fall into the function that checks when the DOM is ready, so the new code looks like this;
    Code:
    $(document).ready(function() {
        $(
    ".zebra tr").mouseover(function() {
            $(
    this).addClass("over");
        });
        $(
    ".zebra tr:even").addClass("alt");
    }); 
    Now comes the explanation. The beginning part of this new code selects all the elements with the class zebra (only our table) then every tr element
    inside that class. Once they are all selected when add a new funtion, mouseover. This will mean when the element is hovered over the function after
    will be ran. In our case. It will run;
    Code:
    $(this).addClass("over"); 
    The selector "this" will select the element last time an element was selected so it will select our table's tr elements. Then it will add the class
    "over" to that element. In summary, the class "over" will be added to the tables tr elements when they are hovered over.

    Now we need to add a new event which will remove the class when the mouse is left. This is basically the same as above, but we will remove the class.
    Code:
    $(".zebra tr").mouseout(function() {
        $(
    $this).removeClass("over")
    }); 
    All this code can be cut down to very few lines. But that is more advanced, probably the hardest jQuery gets, so I will show you in another tutorial.
    If you would like to see how to do it, you can just Google "jQuery chains".

    Hope you enjoyed the tutorial. Tutorial one and two can be found in my signature.

    Good luck

    +rep if you liked.

    Regards,
    Brandon
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: jQuery: Events

    Nicely done! +rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: jQuery: Events

    I like. +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Events

    Thanks Winged and Jordan Step closer to Code Warrior.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  5. #5
    Guru mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim.'s Avatar
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,393

    Re: jQuery: Events

    Perfect ... +rep .. when i Can .!

  6. #6
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Events

    Your welcome mate, take your time
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  7. #7
    Code Warrior Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N's Avatar
    Join Date
    Sep 2008
    Location
    Kosovo
    Age
    18
    Posts
    4,034

    Re: jQuery: Events

    CooL .. +rep too

  8. #8
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Events

    Thanks Egz. You getting interested in jQuery?
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  9. #9
    Guru mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim.'s Avatar
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,393

    Re: jQuery: Events

    Yes ..
    it's Really interesting .. ! but a bit hard ..

  10. #10
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Events

    It will come much easier, at the start it will be bumpy. But after, it should be a breeze for you mate.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. jquery kid
    By amrosama in forum The Lounge
    Replies: 11
    Last Post: 02-27-2009, 03:45 PM
  2. jQuery: Zebra striped table
    By Brandon W in forum Javascript
    Replies: 13
    Last Post: 02-26-2009, 03:29 PM
  3. jQuery: Selectors
    By Brandon W in forum Javascript
    Replies: 8
    Last Post: 02-26-2009, 07:23 AM
  4. Replies: 0
    Last Post: 09-29-2008, 07:14 AM
  5. Replies: 0
    Last Post: 08-25-2007, 10:16 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts