Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-21-2012, 11:12 AM   PM User | #1
unicown
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
unicown is an unknown quantity at this point
beginner: getting position of item in array on mouseover

Hi, I am trying to make it so that when you hover over a specific image, the script grabs the title from that image and places it in a paragraph. At the moment, nothing happens when I hover over any image. I don't fully understand why it's not working but my guess would be it's because I should of specified the img position on mouseover (getImgs[x].onmouseover= imgOnHover. How do I get the position of the item in that array that's being hovered over? is there a better way to write this code without changing the HTML?

Code:
<html>
<head>
<title>blah</title>
<script>
function init() {
var holder = document.getElementById("holder");
var getImgs = holder.getElementsByTagName("img");
var placeHere = document.getElementById("insert_here");

for(x=0; x<getImgs.length; x++){
getImgs[x].onmouseover= imgOnHover;

function imgOnHover() {
var getTitle = getImgs[x].title;
placeHere.innerHTML = getTitle;
}

}

}
window.onload = init;
</script>
</head>
<body>
<div id="holder">
<img src="image.jpg" title="one">
<img src="image.jpg" title="two">
<img src="image.jpg" title="three">
<img src="image.jpg" title="four">
<img src="image.jpg" title="five">
<img src="image.jpg" title="six">
<img src="image.jpg" title="seven">
</div>
<p id="insert_here"></p>
</body>
</html>
unicown is offline   Reply With Quote
Old 11-21-2012, 11:22 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Magic keyword: "this"

Code:
function imgOnHover() {
   var getTitle = this.title;
   placeHere.innerHTML = getTitle;
}
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
unicown (11-21-2012)
Old 11-21-2012, 11:33 AM   PM User | #3
unicown
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
unicown is an unknown quantity at this point
/facepalm

thank you lol
unicown is offline   Reply With Quote
Old 11-21-2012, 12:10 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<script type = "text/javascript">
function getTitle(which) {
var t  = which.title;
alert (t);
}
</script>

<div id="holder">
<img src="image.jpg" title="one" onmouseover = "getTitle(this)">
<img src="image.jpg" title="two" onmouseover = "getTitle(this)">
</div>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 Off

Forum Jump


All times are GMT +1. The time now is 12:08 AM.


Advertisement
Log in to turn off these ads.