Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
Or, use your gamerDNA username: (more...)
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 06-20-2009, 02:36 PM   #1 (permalink)
DisgruntledOrangatang
Registered User
 
Join Date: May 2003
Posts: 1,399
Javacript Question

This is related to my previous thread about the map, but it's a specific question about Javascript so I thought I'd make a new one so it's easier to find for other people or w/e.

Anyway I am trying to do my map thing still, using Javascript.

What I am having happen is when you hover over a state abbreviation (text). The hover-over image of the state appears. I can do this all fine in CSS, but I am trying to use Javascript so I don't have to write a function for each state.

I have a feeling I am using "this" incorrectly. But I want to write 1 onMouseOver and onMouseOut code using "this" so I can apply it to all state elements. Same with Javascript, 1 function for each action that all states can use.

So far this is what I have tried:

HTML tag: (text is display:none right now)

HTML Code:
<div id="texas"></div> <a id="texasText" onMouseOver="hoverOver(this)" onMouseOut="hoverOut(this)">TX</a>
javascript:

Code:
function hoverOver(statename){
	var state = document.getElementById(statename);
	state.style.display = "block";
}

function hoverOut(statename){
	var state = document.getElementById(statename);
	state.style.display = "none";
}
But it doesn't work. I have even tried something super simple like this:

Code:
function hoverOver(statename){
        alert(statename);
{
Doesn't work either, I have written that alert function trying it like 5 different ways, but the output is either null, undefined, or a blank box with nothing in it. How can I do this? I have a feeling I am missing some syntax to use "this" to correctly pull some unique information from the tag.
__________________
"Talk all you want, but when I say I'm going to kill you, there's nothing you can do but die"
DisgruntledOrangatang is offline   Reply With Quote
Old 06-20-2009, 08:01 PM   #3 (permalink)
DisgruntledOrangatang
Registered User
 
Join Date: May 2003
Posts: 1,399
Yeah I know you can do it that way. I was trying to avoid that so I could just copy and paste the onMouseOver onMouseOuts to each tag instead of also having to put the parameter in there as the state name. The weird thing is I have used this in a parameter like that before. For instance in a previous project I have this:

Code:
onKeyPress="return submitenter(this,event)"
and the function:

Code:
function submitenter(myfield,e)
		{
			var keycode;
			
			if(window.event)
			{
				keycode = window.event.keyCode;
			}
			else if(e)
			{
				keycode = e.which;
			}
			else
			{
				return true;
			}
			
			if(keycode == 13)
		   {
		   	myfield.form.submit();
		   	return false;
		   }
			else
			{
		   	return true;
			}
		}
So that worked with this, which is why I am confused and surpised I can't use it as I did in the OP.
__________________
"Talk all you want, but when I say I'm going to kill you, there's nothing you can do but die"
DisgruntledOrangatang is offline   Reply With Quote
Old 06-21-2009, 12:55 AM   #4 (permalink)
niteflyx
Fuckin' 07er
 
niteflyx's Avatar
 
Join Date: May 2007
Location: Philly
Posts: 750
+11 Internets
Hmm, for that example of "this" that you used, was the onkeypress command inside a div that you were referring to? Because in the OP example, your javascript function gets called within the element, not the
element.

Also, in the OP example, you take "this" and take it as just an element ID..

You have getElementById(statename), where statename is an element, not a string. It should be statename.style.display or whatever, I think.


I've had a few drinks but I hope this is good programming logic
niteflyx is offline   Reply With Quote
Old 06-30-2009, 08:15 AM   #5 (permalink)
Elradriendir
Long Time Listener - Old Time GM
 
Join Date: Dec 2003
Location: Virginia, USA
Posts: 14
+0 Internets
'this' can get confusing really quick, but in this case you're using it correctly (but perhaps in the wrong way?).

From the ECMA Spec:
HTML Code:
There is a this value associated with every active execution context. The this value depends on the caller
and the type of code being executed and is determined when control enters the execution context. The
this value associated with an execution context is immutable.
Basically in every situation there is going to be a 'this' value, but it's not always going to be clear what it is. In the OP, 'this' is in the context of the anchor tag. You'll be able to get everything associated with the <a> tag you're hovering over (hovering out, clicking, parent nodes etc.), but it has nothing to do with the DIV.

The quickest way I can see to get this working is to just using naming conventions to solve the problem. If you name your <a> tags in a predictable way, you can avoid hard-coding the parameter:

HTML Code:
<div id="texas"></div> <a id="texas_text" onMouseOver="hoverOver(this);" onMouseOut="hoverOut(this);">TX</a>
Then the javascript becomes:

Code:
function hoverOut(e) {
                var id = e.id.replace('_text','');
                var div = document.getElementById(id);
                div.style.display = 'none';
}

function hoverOver(e) {
                var id = e.id.replace('_text','');
                var div = document.getElementById(id);
                div.style.display = 'block';
}
__________________
Lurker
Elradriendir is offline   Reply With Quote
Old 06-30-2009, 10:51 PM   #6 (permalink)
Luthair
Registered User
 
Join Date: May 2002
Posts: 1,538
Relevant link, though it may not be useful until we see more HTML 5 support: Adblock Plus and (a little) more: Selecting countries on a map in Firefox 3.5
Luthair is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

uberguilds network



All times are GMT -7. The time now is 02:47 AM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6