School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,814 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,686 people online right now. Registration is fast and FREE... Join Now!




onmouseover for multiple div created at runtime

 

onmouseover for multiple div created at runtime, Making sure each dynamically created div has a different onmouseover.

young.steveo

17 May, 2009 - 10:13 AM
Post #1

New D.I.C Head
*

Joined: 14 May, 2009
Posts: 37



Thanked: 1 times
My Contributions
I have a loop that creates multiple divs, and assigns each one it's own id and positioning on the page. Everything is working fine here.

However, I need to have a different onmouseover for each div (I'm going to be using an ajax call inside each onmouseover that pulls a different url depending on the div that you have pointed at)

Here is the snippet of code that I used to add the onmouseover attribute to each div. This section of code is inside a larger loop, so the "div" variable is a unique div on each iteration of the loop:

CODE

div.onmouseover = function() {
    var x1 = (str[0]+3)/9;
    var y1 = (str[1]+3)/9;
    var url = "http://space.mygamehandle.com/scripts/star_name.php?x=" + x1 + "&y=" + y1;
    new Ajax(url, {
        method: 'get',
        update: $('label_texter'),
        onComplete: function() {
            alert(url);
        }
    }).request();
}

The str array is a collection of x and y coordinates that the script at star_name.php uses to determine what information needs to be displayed inside the div "label_texter" in the ajax call. If you are not familiar with the mootools framework, you can ignore most of the code inside the function.

The real problem is that for some reason, all the div's created end up with the exact same thing within the url variable. Even though the str[0] and str[1] variables are changed on each iteration of the loop.

In fact, it seems as though they all end up with the last str array values in the loop.

I'm sure it has something to do with the function or something. not sure where to start fixing it.

User is offlineProfile CardPM
+Quote Post


young.steveo

RE: Onmouseover For Multiple Div Created At Runtime

17 May, 2009 - 02:00 PM
Post #2

New D.I.C Head
*

Joined: 14 May, 2009
Posts: 37



Thanked: 1 times
My Contributions
Okay, I figured out what is happening: the function is being overwritten during each loop. So after the final loop, every onmouseover is calling a function that will output according to the last iteration of the loop.

So... How do I stop that from happening? (I mean, I know I can't stop javascript from overwriting, so how do I achieve what I'm trying to do?)
User is offlineProfile CardPM
+Quote Post

crazyjugglerdrummer

RE: Onmouseover For Multiple Div Created At Runtime

17 May, 2009 - 03:50 PM
Post #3

YOUR AD HERE
Group Icon

Joined: 7 Jan, 2009
Posts: 677



Thanked: 94 times
Dream Kudos: 425
My Contributions
Since you can only assign one function to handle the event, you can just make a new function that calls the old one and the new one. You would need to get the old function from the div of course. Something like:

function() {
calloldone();
callnewone();
}

I'm not sure exactly how this would be done, post your results! biggrin.gif
User is offlineProfile CardPM
+Quote Post

young.steveo

RE: Onmouseover For Multiple Div Created At Runtime

17 May, 2009 - 05:32 PM
Post #4

New D.I.C Head
*

Joined: 14 May, 2009
Posts: 37



Thanked: 1 times
My Contributions
That's an interesting approach that would have probably worked, crazyjugglerdrummer.

What I did to solve the problem:

I changed the function to

CODE
div.onmouseover = function () {
   star_name_pop(this);
};


I set the id of the div to be the x and y coordinates like so: id="234,1009"

Then in the star_name_pop() function I put this:

CODE
    function star_name_pop(div) {
        var coordinates = div.id;
        var str = coordinates.split(/,/g);
        var x1 = ((str[0]*1)+3)/9;
        var y1 = ((str[1]*1)+3)/9;
        var url = "http://space.mygamehandle.com/scripts/star_name.php?x=" + x1 + "&y=" + y1;
        new Ajax(url, {
            method: 'get',
            update: $('label_texter'),
            onComplete: function() {
            }
        }).request();
    }


That got the job done.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:03PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month