3

I am completely baffled as to why this simple jQuery script is not working. I literally copied and pasted the code from another project of mine (in which the code works) and just changed the class tag.

<html>
<head>
    <title>test page</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('.click_item').click(
                function() {
                    $(this).animate({background: '#ffffff'}, 200);
                },
                function() {
                    $(this).animate({background: '#d0d0d0'}, 200);
                }
            );
    }) 
    </script>
    <style type="text/css">
        #one {
            height: 300px;
            width: 1000px;
            background-color: #a0a0a0;
        }
        .click_item {
            width: 100px;
            height: 30px;
            margin: 0px auto;
            margin-bottom: 10px;
            background: #d0d0d0;
            -moz-transition: background linear;
            -webkit-transition: background linear;
        }
    </style>
</head>

<body>

    <div id="one">
        <div id="map_options_box">
            <div id="kml1" class="click_item">item 1</div>
            <div id="kml2" class="click_item">item 2</div>
        </div>
    </div>

</body>

I also tried using the #kml1 and #kml2 ID tags instead of class and 'this' tags, as well as replacing the click function with hover and toggle,still didn't work. I'm not receiving any errors in the console either.

1
  • 1
    You are calling two functions inside jQuery click event and that too changing the background of this element. Which one the function will consider? Call anyone function for the same event for the same element. Commented Sep 14, 2012 at 19:53

2 Answers 2

12

You will need to include the jQuery color animations plugin., because native jquery does not support this feature

Sign up to request clarification or add additional context in comments.

1 Comment

And also be aware that jQuery 1.8 breaks the version that a lot of people were using – if you get console errors about curCSS, then you either need 1.7 or lower, or to update the color plugin.
1

Try:

<script type="text/javascript">
$(document).ready(function() {
        $('.click_item').click(
            function() {
                $(this).animate({background: '#ffffff'}, 200);
            },
            function() {
                $(this).animate({background: '#d0d0d0'}, 200);
            }
        );
});
</script>

6 Comments

The last semicolon doesn't make a difference. But, you can't use multiple functions on a click event.
I didn't think so, I thought that looked funny.
Should that comma be replaced with a semicolon?
Multiple functions in a click event aren't allowed and don't make sense anyway. Plus that's not the core problem. mgraph got it.
Multiple functions do make sense, mouseup and mousedown. The same as a hover event (mouseover and mouseout). And, yes, mgraph got it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.