jQuery overlay on multiple divs..only working on first div...

I've got a page with multiple divs indicating a news item in a CMS i'm building.

Each news item has it's on div called #new-item and is wrapped in it.

Inside that div is the content for the item and a div with a class of .new-item-overlay which has a 0 opacity upon loading.

My jquery is set to reveal the overlay which will indicate that clicking on it at that point will bring up editing options.

THE PROBLEM IS THAT ONLY THE FIRST NEWS-ITEM DIV ON THE PAGE IS GETTING THE ACTION TO ACTIVATE THE OVERLAY...

<script language="javascript">
$(document).ready(function(){
	
$("div.news-item-overlay").hover(
function() {
$(this).animate({"opacity": "0.60"}, "fast");
},
function() {
$(this).animate({"opacity": "0"}, "fast");
});			  		   		   
</script>

Open in new window



My html has divs like the following:

<div id="news-item">
      <div class="news-item-overlay">edit this item</div>
      <img width="100" height="100" />
      <p>News Item Content - Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
    <div id="news-item"><div class="news-item-overlay">edit this item</div><img width="100" height="100" />
      <p>News Item Content - Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>

Open in new window


The css for #news-item and .news-item-overlay is:

#news-item {
	float:left;
	width:875px;
	height:150px;
	margin:25px 0px 0px 25px;
	background-color:#333;
	cursor:pointer;
}
.news-item-overlay {
	position:absolute;
	left:0px;
	top:0px;
	width:875px;
	height:150px;
	margin:25px 0px 0px 25px;
	background-color:#000;
	z-index:2;
	cursor:pointer;
	color:#FFF;
	line-height:150px;
}

Open in new window


I'm guessing there is something i'm missing that's staring me right in the face...anybody know what's up?
dvointeractiveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
McOzCommented:
The issue is that you have multiple divs with the same ID ("news-item"). This is causing the problem, as IDs are supposed to be unique.

Use classes instead, like
<div class="news-item">
      <div class="news-item-overlay">edit this item</div>
      <img width="100" height="100" />
      <p>News Item Content - Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
    <div class="news-item"><div class="news-item-overlay">edit this item</div><img width="100" height="100" />
      <p>News Item Content - Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>

Open in new window


and  
.news-item {
        float:left;
        width:875px;
        height:150px;
        margin:25px 0px 0px 25px;
        background-color:#333;
        cursor:pointer;
}

Open in new window


Cheers
0
 
McOzCommented:
Or else have unique IDs for each div, otherwise they would all open at once? :-)
0
 
dvointeractiveAuthor Commented:
@McOz

That still does not seem to work, I get the same result. I switched the css and the html to call the class instead of id...I get the same result: only the first div gets an overlay on hover...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
liveaspankajCommented:
class is a good point +
also try changin the follow at line 4:(remove the div)

$(".news-item-overlay").hover(
0
 
dvointeractiveAuthor Commented:
I tried targeting the child div (the overlay) of the current hover too...didn't work. Though now the second div in the page activates the first divs overlay when hovered on too.

$(".news-item").hover(
function() {
$(this).children("div").animate({"opacity": "0.60"}, "fast");
},
function() {
$(this).children("div").animate({"opacity": "0"}, "fast");
});

Open in new window

0
 
liveaspankajCommented:
do you want effect on "edit this item" or the content in <p> or all?
0
 
dvointeractiveAuthor Commented:
@liveaspankaj

On the overlay and aything contained within it.
0
 
liveaspankajCommented:
the overlay div contains only the message "edit this item"

if you want everything you should do this:


$(".news-item").hover(
function() {
$(this).animate({"opacity": "0.60"}, "fast");
},
function() {
$(this).animate({"opacity": "0"}, "fast");
});


if you want only the message in the <p> tag:

$(".news-item").hover(
function() {
$(this).children("p").animate({"opacity": "0.60"}, "fast");
},
function() {
$(this).children("p").animate({"opacity": "0"}, "fast");
});
0
 
dvointeractiveAuthor Commented:
liveaspankaj

Doesn't work.

Here's a live sample of what's going on - http://hover.dvointeractive.com/

The way the overlay works on the first item is perfect.

Notice that hovering on the second item activates the overlay on the first item instead of itself...

That's what needs to be fixed.
0
 
liveaspankajCommented:
i found the problem.


.news-item-overlay {
      position:relative;
      left:0px;
      top:0px;
      width:875px;
      height:150px;
      margin:25px 0px 0px 25px;
      background-color:#000;
      z-index:2;
      cursor:pointer;
      color:#FFF;
      line-height:150px;
}

the position in the css above has to be relative and not absolute. otherwise all the elements will pileup the overlays at the sameplace. then you will have to do some css to make it look proper. but its sure that you cannot use absolute position for all the overlay elements
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.