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.
elepilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
It appears that the mouseup(function() is attached to "#testDiv" and not the appended <div>s.  I can click anywhere on the "#testDiv" and it responds, not just in the appended 'divs'.
0
Tom BeckCommented:
What Dave said. Corrected code:
$(function() {
		var obj1 = $("<div class='testItem'>Test Item 1</div>");
                $("#testDiv").append(obj1);
                obj1.mouseup(function() {
                    location.href = "http://www.amazon.com";
                });
                var obj2 = $("<div class='testItem'>Test Item 2</div>");
		$("#testDiv").append(obj2);
                obj2.mouseup(function() {
                    location.href = "http://www.ebay.com";
                });
            });

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  Although I spotted the problem, Tom knew how to fix it.
0
Tom BeckCommented:
Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.