|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Rate Thread | Display Modes |
| | #1 (permalink) |
| 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> 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";
} Code: function hoverOver(statename){
alert(statename);
{
__________________ "Talk all you want, but when I say I'm going to kill you, there's nothing you can do but die" |
| | |
| | #3 (permalink) |
| 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)" 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;
}
}
__________________ "Talk all you want, but when I say I'm going to kill you, there's nothing you can do but die" |
| | |
| | #4 (permalink) |
| Fuckin' 07er 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 ![]() |
| | |
| | #5 (permalink) |
| Long Time Listener - Old Time GM Join Date: Dec 2003 Location: Virginia, USA
Posts: 14
| '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. 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> 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 |
| | |
| | #6 (permalink) |
| 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 |
| | |
![]() |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
| |