Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

Weird jQuery/JavaScript problem.

I set up two mouseup() event handlers for two separate objects, each of which when clicked, is supposed to go to two different websites. The first object, when clicked, should go to amazon.com, and the second should go to ebay.com. But somehow, both keep going to ebay. Can someone please tell me why this is happening?

Here is the code:

<html>
    <head>
        <style>

            html {
                min-width: 300px;
                height: 100%;
            }

            #testDiv {
                margin: 0 auto;
                display: inline-block;
                background: red;
                border: 3px solid white;
                width: 300px;
                height: 300px;
            }
            
            .testItem {
                width: 50px;
                height: 50px;
                background: green;
                border: 1px solid white;
                cursor: pointer;
            }

        </style>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script>
            $(function() {
                var obj1 = $("#testDiv").append("<div class='testItem'>Test Item 1</div>");
                obj1.mouseup(function() {
                    location.href = "http://www.amazon.com";
                });
                var obj2 = $("#testDiv").append("<div class='testItem'>Test Item 2</div>");
                obj2.mouseup(function() {
                    location.href = "http://www.ebay.com";
                });
            });
        </script>
    </head>
    <body>
        <div id="testDiv">

        </div>
    </body>
</html>

Open in new window


Thanks.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil
elepil

ASKER

Damn, I've been working on this application for the past 10 hours, I'm getting blind, lol.

Thank you for your help. More points given to Tom Beck because he provided code correction, even though Dave spotted the problem first.
You're welcome.  Although I spotted the problem, Tom knew how to fix it.
Thanks for the points.