The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Two seperate div elements with connecting links..., Two seperate div elements with links but activating both on mouse over
Graphicsgod
post Jan 5 2012, 02:53 PM
Post #1





Group: Members
Posts: 3
Joined: 5-January 12
Member No.: 16,195



I'm pulling my hair out trying to figure out how to get this simple thing to work.. I don't want to resort to reworking my div's to make what I want work. I just feel like this should work, but it just doesn't...

So anyway here is what I have:

CODE
<div><a href=""><img src=""></a></div>
<div><a href=""><img src=""></a></div>
<div><a href=""><img src=""></a></div>
<div><a href=""><img src=""></a></div>
<div><a href=""><img src=""></a></div>

<div><a href="">Text Here</a></div>
<div><a href="">Text Here</a></div>
<div><a href="">Text Here</a></div>
<div><a href="">Text Here</a></div>


Now I want that when you mouse over the image it also effects the text link. I know I can easily just combine the image and the text together and it will work, but that's not what I want to do. I tried mixing css and javascript, but it so far only works when the two elements are in the same div. Now technically it should still work even when they are separated because it is just calling for the script. So it shouldn't matter where the two elements are, right?!? But apparently I can only get it to work when they are together also defeating the purpose.

Here is what I tried doing with java script:

WORKS:
CODE
<script language="javascript">
function hightlight()
{
    
    document.getElementById("textspan").style.color = "#a3a2a2";
    document.getElementById("imgsrc").style.border = "0px solid blue";
    //document.getElementById("textspan").setStyle("color","blue");
    //document.getElementById("imgsrc").setStyle("border","0px solid blue");
}
function removehightlight()
{
    document.getElementById("textspan").style.color = "#079ce0";
    document.getElementById("imgsrc").style.border = "0px solid blue";
}
</script>

<div style=onmouseover="hightlight()" onmouseout="removehightlight()"><span id="textspan" >This is a test div to show mouseover</span><img id="imgsrc" src="/images/test.gif" /></div>


So if I add the style command to another div, nothing happens. WTF?!?!?!?!?!?

DOESN'T WORK:
CODE
<script language="javascript">
function hightlight()
{
    
    document.getElementById("textspan").style.color = "#a3a2a2";
    document.getElementById("imgsrc").style.border = "0px solid blue";
    //document.getElementById("textspan").setStyle("color","blue");
    //document.getElementById("imgsrc").setStyle("border","0px solid blue");
}
function removehightlight()
{
    document.getElementById("textspan").style.color = "#079ce0";
    document.getElementById("imgsrc").style.border = "0px solid blue";
}
</script>

<div style=onmouseover="hightlight()" onmouseout="removehightlight()">
<img id="imgsrc" src="/images/test.gif" />
</div>
<div style=onmouseover="hightlight()" onmouseout="removehightlight()">
<a href="#" id="textspan" />Text Here</a>
</div>


Shouldn't this work?!?! I don't know why it shouldn't, I'm going to take a break from it for a min. Anyone who knows xhtml, tell me why I'm an idiot wink.gif.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 5 2012, 03:17 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



This is incorrect:
CODE
<div style=onmouseover="hightlight()"

(remove the style= part).

Side-notes:

- Note that a DIV by default can be is as wide as the browser window, so the mouseout event can trig even if the cursor is to the right of the image:
CODE
onmouseout="removehightlight()">
<img id="imgsrc" src="/images/test.gif" />
</div>

(you might put the mouse events in the IMG and A elements instead, there's no real need for the DIVs).

- An IMG elements needs an ALT attribute.

- This is a little overkill:
CODE
document.getElementById("imgsrc").style.border = "0px solid blue";

(a zero px wide border doesn't need color or border-style).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Graphicsgod
post Jan 5 2012, 04:18 PM
Post #3





Group: Members
Posts: 3
Joined: 5-January 12
Member No.: 16,195



QUOTE(Christian J @ Jan 5 2012, 03:17 PM) *

This is incorrect:
CODE
<div style=onmouseover="hightlight()"

(remove the style= part).

Side-notes:

- Note that a DIV by default can be is as wide as the browser window, so the mouseout event can trig even if the cursor is to the right of the image:
CODE
onmouseout="removehightlight()">
<img id="imgsrc" src="/images/test.gif" />
</div>

(you might put the mouse events in the IMG and A elements instead, there's no real need for the DIVs).

- An IMG elements needs an ALT attribute.

- This is a little overkill:
CODE
document.getElementById("imgsrc").style.border = "0px solid blue";

(a zero px wide border doesn't need color or border-style).



Awesome!! Thank you for showing me what I was doing wrong smile.gif! Also I am trying to get each links and images to be separate. Right now all the links are acting together. I mouse over one image all the text links get highlighted. I tried adding commands to the javascript to separate them, but they still all highlight together. Do I have to create separate javascript events for each set of links?

Example:

CODE
<script language="javascript">
function hightlight()
{
    
    document.getElementById("textspan1").style.color = "#0091d8";
    document.getElementById("imgsrc1").style.border = "0px solid blue";
    document.getElementById("textspan2").style.color = "#0091d8";
    document.getElementById("imgsrc2").style.border = "0px solid blue";
    document.getElementById("textspan3").style.color = "#0091d8";
    document.getElementById("imgsrc3").style.border = "0px solid blue";
    document.getElementById("textspan4").style.color = "#0091d8";
    document.getElementById("imgsrc4").style.border = "0px solid blue";
    //document.getElementById("textspan").setStyle("color","blue");
    //document.getElementById("imgsrc").setStyle("border","1px solid blue");
}
function removehightlight()
{
    document.getElementById("textspan1").style.color = "#a3a2a2";
    document.getElementById("imgsrc1").style.border = "0px solid blue";
    document.getElementById("textspan2").style.color = "#a3a2a2";
    document.getElementById("imgsrc2").style.border = "0px solid blue";
    document.getElementById("textspan3").style.color = "#a3a2a2";
    document.getElementById("imgsrc3").style.border = "0px solid blue";
    document.getElementById("textspan4").style.color = "#a3a2a2";
    document.getElementById("imgsrc4").style.border = "0px solid blue";
}
</script>

<div><a href=""><img src="" id="imgsrc1"  onmouseover="hightlight()" onmouseout="removehightlight()" /></a></div>
<div><a href=""><img src="" id="imgsrc2"  onmouseover="hightlight()" onmouseout="removehightlight()" /></a></div>
<div><a href=""><img src="" id="imgsrc3"  onmouseover="hightlight()" onmouseout="removehightlight()" /></a></div>
<div><a href=""><img src="" id="imgsrc4"  onmouseover="hightlight()" onmouseout="removehightlight()" /></a></div>

<div><a href="" id="textspan1"  onmouseover="hightlight()" onmouseout="removehightlight()" />Text Here</a></div>
<div><a href="" id="textspan2"  onmouseover="hightlight()" onmouseout="removehightlight()" />Text Here</a></div>
<div><a href="" id="textspan3"  onmouseover="hightlight()" onmouseout="removehightlight()" />Text Here</a></div>
<div><a href="" id="textspan4"  onmouseover="hightlight()" onmouseout="removehightlight()" />Text Here</a></div>




The reason I have the divs setup this way is to create a table look. And sorry for the "opx solid blue", left it blank while I sort out this coding mess.. Then I'll re-enable it.

Thank you for all your help!!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Graphicsgod
post Jan 5 2012, 04:40 PM
Post #4





Group: Members
Posts: 3
Joined: 5-January 12
Member No.: 16,195



After playing around I got this quick work around I have this:

CODE
<script language="javascript">
function hightlight1()
{
    
    document.getElementById("textspan1").style.color = "#0091d8";
    document.getElementById("imgsrc1").style.border = "0px solid blue";
    //document.getElementById("textspan").setStyle("color","blue");
    //document.getElementById("imgsrc").setStyle("border","1px solid blue");
}
function removehightlight1()
{
    document.getElementById("textspan1").style.color = "#a3a2a2";
    document.getElementById("imgsrc1").style.border = "0px solid blue";
}
function hightlight2()
{
    
    document.getElementById("textspan2").style.color = "#0091d8";
    document.getElementById("imgsrc2").style.border = "0px solid blue";
    //document.getElementById("textspan").setStyle("color","blue");
    //document.getElementById("imgsrc").setStyle("border","1px solid blue");
}
function removehightlight2()
{
    document.getElementById("textspan2").style.color = "#a3a2a2";
    document.getElementById("imgsrc2").style.border = "0px solid blue";
}
</script>


It's probably not the right way to code this, but so far it's doing what I need..
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 5 2012, 05:27 PM
Post #5


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



The last example should work fine.

To make the code shorter you can instead use a single function with parameters/arguments to separate the different links and images. See e.g. http://www.quirksmode.org/js/function.html
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th April 2024 - 09:58 AM