dynamic onMouseover ? ...

DrEvil

FCRI Associate!
Reaction score
111
Ok so I didn't really know what to call the thread, but more to the point!

I have a load function that sets the default values on 5 fields, and adds an onmouseover event but the event everytime has the same parameter...

PHP:
while(i<=5){
		id='P'+i.toString();
		d=document.getElementById('c'+id);//.innerHTML="Not Started";
        // The 'status' box
		d.innerHTML="Not Started";
		d.style.backgroundColor="#FFFF00";

		d=document.getElementById(id);
        // The actual 'name' of the box
		d.onmouseover=function(){hoverA(id);};
		//alert(id);
		i++;
	}

hoverA(id) just shows the id (which I will change later...)
but it always shows the last value 'P5'
I'm sure I'm doing something really stupid wrong... but I can't figure it out...

thanks for helping :)
 

UndeadDragon

Super Moderator
Reaction score
447
Can I see the little bit of the HTML too? I'm guessing that there is just 2 divs, one called "cPX" and one that is just "PX"?

Plus, I don't know if it would help to null d each time. It may be retaining a value.

Another thing to try would be to alert() each value on each loop to see where the error may be.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
nvm :D

id='P'+i.toString();
id is defined before or you forgot "var"?

samething with "i" and why aren't you using a for-loop?
 

DrEvil

FCRI Associate!
Reaction score
111
PHP:
//

	var i=1;
	var d;
	var id;//='P'+i.toString();
//...

  <div class="cell" id="P1">Physics 1</div>
  <div class="completed" id="cP1">&nbsp;</div>
  
  <div class="cell" id="P2">Physics 2</div>
  <div class="completed" id="cP2">&nbsp;</div>

I wanted to have it so when the user hovers over any of the 'cell' classes it sends that specific id to the function to show more information
While 'completed' class is to show 'not started' 'in progress' 'finished'

Using alert(id) shows it right 'P1','P2'... 'P5'
If I null both of them at the end it always shows 'null' once hovered over
It seems as if the function retains the last value of id...
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
That's because the context created by function(){hoverA(id);}; refers to the variable id and not the value of the variable id at time of creation. You have to create a distinct variable for each onmouseover. I'm not entirely sure of javascripts scoping rules but either of these two solutions should work:

Code:
while(i<=5){
       [B]var[/B] id='P'+i.toString();
        d=document.getElementById('c'+id);//.innerHTML="Not Started";
        // The 'status' box
        d.innerHTML="Not Started";
        d.style.backgroundColor="#FFFF00";

        d=document.getElementById(id);
        // The actual 'name' of the box
        d.onmouseover=function(){hoverA(id);};
        //alert(id);
        i++;
    }

Code:
while(i<=5){
        id='P'+i.toString();
        d=document.getElementById('c'+id);//.innerHTML="Not Started";
        // The 'status' box
        d.innerHTML="Not Started";
        d.style.backgroundColor="#FFFF00";

        d=document.getElementById(id);
        // The actual 'name' of the box
        (function(id) {
              d.onmouseover=function(){hoverA(id);};
        })(id);
        //alert(id);
        i++;
    }

A nicer solution is to extract the content of the while loop to its own function.
 

DrEvil

FCRI Associate!
Reaction score
111
Ok thanks phyrex1an, tbh all I really needed to know if it was a reference or value and I guess now I know it stores a reference ( which a bit of trial and error could've shown me... but I was lazy... )

I guess I will clean it up to a function for registering blah... blah...

Code:
//
while(..){
registerHover(id);
}

functions inside functions.. kindof confusing but should be learn'able :D

Thanks for all the help guys :)
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
It always shows the last value because your anonymous function only gets called on mouseover.
Then it will look up the variable id in its scope chain, ending up in your main function's scope.

id will then be 5, because the anonymous function only fires after setup function has already run.

Phyrexian's solution is correct, as by using an IIFE,
you pass the current value of id as a parameter to set the onmouseover event with


Ok thanks phyrex1an, tbh all I really needed to know if it was a reference or value and I guess now I know it stores a reference

JavaScript never passes references, always values.
With Object it seems to pass a reference, but it actually passes the value of the stack item that behaves as a reference to the actual object on the heap.

You didn't get this problem because you passed by reference, you got the problem because JS will only look up the value of id when the mouseover fires.
That will always be 5, since the variable 'id' in the setup function's scope chain ended at 5.

EDIT
------

Alternatively, have you tried this:
PHP:
while(i<=5){
        var id='P'+i,
        doc=document,
        d=doc.getElementById('c'+id);

        d.innerHTML="Not Started";
        d.style.backgroundColor="#FFFF00";

        d=doc.getElementById(id);
        d.onmouseover=function(){hoverA(this.id);};

        i++;
    }

  • prevents vars from being global
  • caches document for performance
  • doesn't use an IIFE
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top