Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

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 10-20-2008, 05:54 PM   PM User | #1
canadiantodd80
New to the CF scene

 
Join Date: Oct 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
canadiantodd80 is an unknown quantity at this point
Noob question about DOM and images

Hi guys n gals... hoping someone can help me with this.

I have a main image in my page that changes based on what the user
has selected in a dragdown box (<select><option>) type setup. That all works fine but I have another little image that says "INFO" and the onClick event for it is supposed to grab the document.getElementById("myimage").src and pass it to my identifypicture() function.

This all works great and the alert box I want shows up just fine, except that it always takes the .src which is hardcoded into the HTML document that shows up by default when a user loads the page. If the picture gets changed via the dropdown box, the old .src of the default image is still picked up via the DOM. Is there a way to use the DOM to grab the .src of the image that is currently being displayed?

Hopefully I've explained this well enough, and my thanks in advance for your help.

Cheers,
Todd
canadiantodd80 is offline   Reply With Quote
Old 10-21-2008, 03:36 AM   PM User | #2
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
Show us the codes and markups involved.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Users who have thanked rangana for this post:
canadiantodd80 (10-21-2008)
Old 10-21-2008, 03:42 AM   PM User | #3
canadiantodd80
New to the CF scene

 
Join Date: Oct 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
canadiantodd80 is an unknown quantity at this point
Thanks, I'll drop it in when I get back to class tomorrow as I have the code stored there.
canadiantodd80 is offline   Reply With Quote
Old 10-21-2008, 04:35 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,462
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
if the code reads the .src from the dom, it should reflect the current file you see displayed.

you might be accessing it upon bootup and memorizing it at that time, while it's still the default value.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 10-21-2008, 12:22 PM   PM User | #5
canadiantodd80
New to the CF scene

 
Join Date: Oct 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
canadiantodd80 is an unknown quantity at this point
Alright I'm back and here's the code I'm working on.. hopefully it helps. It seems pretty basic but I can't wrap my head around why it won't grab the current .src, only the hardcoded one...

Select Scale: <select name="scale" id="scaleselect" onChange="chgMap(this.options[this.selectedIndex].value)">
<option id="500000" value="images/500000.jpg" selected="selected">1 : 500,000</option>
<option id="250000" value="images/250000.jpg">1 : 250,000</option>
<option id="50000" value="images/50000.jpg">1 : 50,000</option>
<option id="10000" value="images/10000.jpg">1 : 10,000</option>
<option id="2000" value="images/2000.jpg">1 : 2,000</option>
</select>

So this creates my dropdown box that allows me to change images between the different scales based on which option is selected... so far so good.

<img src="images/500000.jpg" width="720px" height="480px" name="mapimg" id="mapimg" />

My image hardcoded to display 500000.jpg by default... this is the .src that is always passed to my next function coming up... even if the picture has been changed using the dropdown box above.

<img border="0px" src="images/identify.gif" onClick="metaData(document.getElementById('mapimg').src)" id="ident" name="ident" />

So this is a little pic that says "Get Info" and when clicked, passes the .src of the displayed 'mapimg' picture to my metaData() function... this is where I think my problem lies, but I have no idea why...

And finally.. my function which takes the .src as an argument and uses a bunch of if/else ifs to see what .src it is and alert box accordingly.

function metaData(whichMap) {
if (whichMap = "images/500000.jpg") {
alert("Insert data here...");
}
else if (whichMap = "images/250000.jpg") {
alert("Insert data 2 here...");
}
else if (whichMap = "images/50000.jpg") {
alert("Insert data 3 here...");
}
else if (whichMap = "images/10000.jpg") {
alert("Insert data 4 here...");
}
else {
alert("Insert data 5 here...");
}
}

As you can see it's pretty simple stuff, it's only an intro to JScript course but for some reason this doesn't want to link the correct .src to my function as it always displays the result for the 1st IF statement. I'm stumped... anyone have any ideas?

Cheers
Todd
canadiantodd80 is offline   Reply With Quote
Old 10-21-2008, 04:00 PM   PM User | #6
fishluvr
Regular Coder

 
fishluvr's Avatar
 
Join Date: Nov 2005
Posts: 110
Thanks: 1
Thanked 12 Times in 12 Posts
fishluvr is on a distinguished road
= (assignment operator -- myVar = "1")
== (is equivalent to -- myVar == 1 // true)
=== (equals strictly -- myVar === 1 // false)
__________________
><((((º>`·.¸¸.·´¯`·.¸.·´¯`·...¸><((((º>`·.¸¸.·´¯`·.¸¸.·´¯`·.. ><((((º>`·.¸¸.·´¯`·.¸.·´¯`·...¸><((((º>
fishluvr is offline   Reply With Quote
Old 10-23-2008, 02:33 PM   PM User | #7
canadiantodd80
New to the CF scene

 
Join Date: Oct 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
canadiantodd80 is an unknown quantity at this point
Boy, do I feel stupid.. should have saw that as I have similar code in the page that uses the "==" comparison.

Thanks for pointing it out for me though... an extra set of eyes always helps!
canadiantodd80 is offline   Reply With Quote
Old 10-26-2008, 07:09 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
That's why below is a good programming practice to avoid that equality operator typo.
Code:
if (1 == myVar){
  ...
}
If in case you had if (1 = myVar), it will throw a runtime error and immediately alerts the developer that there's something wrong in the code.

BTW, if you access the src of an image, it will give you the absolute path, so the code should check for occurrence of the filename and not equality.

Code:
if (whichMap.indexOf("images/500000.jpg") != -1) {
   alert("Insert data here...");
}
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app

Last edited by glenngv; 10-26-2008 at 07:12 AM..
glenngv 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 09:07 AM.


Advertisement
Log in to turn off these ads.