Forum Moderators: not2easy

Message Too Old, No Replies

Accessing Parent DIVS?

         

scooter41

9:46 am on Apr 25, 2007 (gmt 0)

10+ Year Member



Hi there, is it possible to access DIVS using some kind of parent rule? I have a hyperlink for the following picture thumbnail viewer, where you can add the picture to favourites, and the hyperlink turns green onclick, but I would love to be able to access the parent div and turn the background colour green also, is this possible?

I know you could do a getElementByID but there could be 80 or so divs on the page at once and think that would get rather messy.

Essentially I want to do something like parent.div.style.backgroundcolor ....... but syntactically correct in javascript I expect?

Heres some code... 2 examples of the thumbnail box:


<div class="photobox">
<div class="imgT"><img src="/previews/dannip/top20/thumbs/Danni_Top20_14.jpg" ></div>
<div class=buyLink2 >file: <b>Danni Top20 14</b></div>
<div class=buyLink ><a href=# onClick="addToBasket('Danni_Top20_14.jpg','71717','top20');this.style.color='#b5ff0d';return false" >+ADD TO FAVS</a></div></div>

<div class="photobox">
<div class="imgT" ><img src="/previews/dannip/top20/thumbs/Danni_Top20_15.jpg" ></div>
<div class=buyLink2 >file: <b>Danni Top20 15</b></a></div>
<div class=buyLink ><a href=# onClick="addToBasket('Danni_Top20_15.jpg','71717','top20');this.style.color='#b5ff0d';return false" >+ADD TO FAVS</a></div></div>

Thanks for any help in advance!

Robin_reala

12:15 pm on Apr 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



CSS doesn't have a parent selector and probably won't do in the forseeable future if the discussions on www-style are anything to go by. You can however do what you want in Javascript. Seeing as you're already setting colours in there let's just tag some code onto the end:

<div class="photobox">
<div class="imgT"><img src="/previews/dannip/top20/thumbs/Danni_Top20_14.jpg" ></div>
<div class=buyLink2 >file: <b>Danni Top20 14</b></div>
<div class=buyLink ><a href=# onClick="addToBasket('Danni_Top20_14.jpg','71717','top20');this.style.color='#b5ff0d';this.parentNode.style.backgroundColor="#b5ff0d";return false" >+ADD TO FAVS</a></div></div>

That's untested but shouldn't cause any problems.

scooter41

12:35 pm on Apr 25, 2007 (gmt 0)

10+ Year Member



thats superb! perfect and just what the doctor ordered thank you!

Dabrowski

1:00 pm on Apr 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have used this style before, I found an easier way which you may be able to use...

I'm going to assume that class buyLink is basically a sized box of 150px by 30px. By setting it to display block it's basically a DIV and an A in one.


.photobox A {
display: block;
width: 150px; height: 30px;
}

.photobox A.buylink { background: lightblue; }
.photobox A.buyLink:hover { background: coral; }
.photobox A.buyClicked { background: lightgreen; }

<a class='buyLink' href='#' onClick="addToBasket('Danni_Top20_14.jpg','71717','top20'); this.className = 'buyClicked'; return false;" >+ADD TO FAVS</a>

Note you can removed the A's parent DIV this way. Here's a C&P test page....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<title>Test Page</title>
<style>

* { font-family: "Arial", sans-serif; }

.photobox A {
display: block;
width: 150px; height: 20px;
margin-bottom: 2px; padding: 5px;
line-height: 20px;
}

.photobox A.buyLink { background: lightblue; }
.photobox A.buyLink:hover { background: coral; }
.photobox A.buyClicked { background: lightgreen; }

</style>
</head>

<body>

<div class='photobox'>
<a class='buyLink' href='#' onClick="this.className = 'buyClicked'; return false;" >Link 1</a>
<a class='buyLink' href='#' onClick="this.className = 'buyClicked'; return false;" >Link 2</a>
<a class='buyLink' href='#' onClick="this.className = 'buyClicked'; return false;" >Link 3</a>
<a class='buyLink' href='#' onClick="this.className = 'buyClicked'; return false;" >Link 4</a>
</div>

</body>
</html>