I don't get what's wrong here...

M

Marc Miller

I have an html img with a source of 'lbug.gif'. I want to be able to change
out the source of the img by clicking a button. Here's my code:

function swapPic()
{
if (document.Form1.imgBug.src = "lbug.gif")
{
document.Form1.imgBug.src = "stopbug.jpg";
}
else if (document.Form1.imgBug.src = "stopbug.jpg")
{
document.Form1.imgBug.src = "lbug.gif"
}
}

When I click the button the 1st time, the code executes as expected. Each
successive
time I click the button, the same code executes which is the:

if (document.Form1.imgBug.src = "lbug.gif")
{
document.Form1.imgBug.src = "stopbug.jpg";
}

And the source never switches back to 'lbug.gif'


What concept am I missing here?

TIA,
Marc Miller
'Confused in Northeast Penna.'
 
T

Thomas 'PointedEars' Lahn

Marc said:
When I click the button the 1st time, the code executes as expected. Each
successive time I click the button, the same code executes which is the:

if (document.Form1.imgBug.src = "lbug.gif")
{
document.Form1.imgBug.src = "stopbug.jpg";
}

And the source never switches back to 'lbug.gif'


What concept am I missing here?

The difference between an assignment "=" and a comparison "==",
and a reasonable Subject header.

<URL:http://jibbering.com/faq/>


PointedEars
 
M

Marc Miller

Thomas 'PointedEars' Lahn said:
The difference between an assignment "=" and a comparison "==",
and a reasonable Subject header.

<URL:http://jibbering.com/faq/>


PointedEars


Thomas,

Thanks for your response and you're right, a reasonable subject header is
much more appropriate.

Interestingly enough, I used the comparasion "==" in my first attempt ( in
the 'if' and 'else if' lines) and nothing happened.

I then changed it to the assignment "=" and, voila, the 'if' line works all
the time, except that it does not
correctly identify the 'src' attribute of the image.

I tried using 'switch' but that seems not to be an option at all.

By the way, my platform it asp.net 2003, .NET framework 1.1.

Thank again,
Marc Miller
 
J

jshanman

Marc said:
Thomas,

Thanks for your response and you're right, a reasonable subject header is
much more appropriate.

Interestingly enough, I used the comparasion "==" in my first attempt ( in
the 'if' and 'else if' lines) and nothing happened.

I then changed it to the assignment "=" and, voila, the 'if' line works all
the time, except that it does not
correctly identify the 'src' attribute of the image.

I tried using 'switch' but that seems not to be an option at all.

By the way, my platform it asp.net 2003, .NET framework 1.1.

Thank again,
Marc Miller

Use the "==" operator, then send an alert if it is not true so you can
see what the value is. It may be adding
"http://www.yourdomain.com/your/path/to/image" to the string as it
loads the image. Thus you would need:

if (document.Form1.imgBug.src ==
"http://www.yourdomain.com/your/path/to/image/lbug.gif") {
document.Form1.imgBug.src =
"http://www.yourdomain.com/your/path/to/image/stopbug.jpg";
}

- JS
http://www.endeavorpub.com
 
R

Randy Webb

Marc Miller said the following on 4/24/2006 1:41 PM:
I have an html img with a source of 'lbug.gif'. I want to be able to change
out the source of the img by clicking a button. Here's my code:

function swapPic()
{
if (document.Form1.imgBug.src = "lbug.gif")

Is imgBug the ID/NAME of your img element or is the name of an input
element in your form named Form1?

Second: Your if test is not testing for equality, it is testing to see
if it can set the .src property. It can so it passes the if test.

Third: Access images through the images collection.

Fourth: The .src of an img tag is not the filename. It is the fully
qualified path to the image file.

function swapPic(){
if (document.images['imageNAME'].src.indexOf('lbug.gif') != -1)
{
document.images['imageNAME'].src = 'stopbug.gif';
}
else
{
document.images['imageNAME'].src = 'lbug.gif'
}
}

But even simpler:

function swapPic(){
document.images['imagename'].src=document.images['imagename'].src.match('lbug.gif')?'stopbug.gif':'lbug.gif';
}
 
M

Marc Miller

jshanman said:
Use the "==" operator, then send an alert if it is not true so you can
see what the value is. It may be adding
"http://www.yourdomain.com/your/path/to/image" to the string as it
loads the image. Thus you would need:

if (document.Form1.imgBug.src ==
"http://www.yourdomain.com/your/path/to/image/lbug.gif") {
document.Form1.imgBug.src =
"http://www.yourdomain.com/your/path/to/image/stopbug.jpg";
}

- JS
http://www.endeavorpub.com


JS,

That does work, and with the '==' operator and the fully qualified path. I
was
trying to avoid having to change the domain in the path however, to save the
necessity of changing the string every time there is a version
change/testing/and move
to production.

Thanks to all!

Marc Miller
 
R

Randy Webb

jshanman said the following on 4/24/2006 2:36 PM:

Use the "==" operator, then send an alert if it is not true so you can
see what the value is. It may be adding
"http://www.yourdomain.com/your/path/to/image" to the string as it
loads the image. Thus you would need:

It does. The .src property of an img object is always a fully qualified
path when your read its .src property.

You don't have to include the fully qualified path when setting the
..src, the browser will do it for you. But, see my other reply :)
 
R

Randy Webb

Marc Miller said the following on 4/24/2006 2:45 PM:

That does work, and with the '==' operator and the fully qualified path. I
was trying to avoid having to change the domain in the path however, to save
the necessity of changing the string every time there is a version
change/testing/and move to production.

You don't have to change it everytime if you dont worry with the domain
aspect. See my other reply:

function swapPic(){
document.images['imagename'].src=document.images['imagename'].src.match('lbug.gif')?'stopbug.gif':'lbug.gif';
}
 
M

Marc Miller

Thanks Randy! Now that's elegant and I've already
put it in my 'code cupboard' for future use as well.

Also thanks again to Mr. PointedEars and jshanman.

Marc Miller
a.k.a. 'Shaky Slidewell'
(ps- don't ask :cool:)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 24 Apr
2006 20:06:23 remote, seen in Thomas
'PointedEars' Lahn said:
PointedEars


In your case, that's bad advice.

The material after your "-- " line does not comply with FYI28/RFC1855.
Please rectify your habits.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top