Connecting Tech Pros Worldwide Help | Site Map

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

Marc Miller
Guest
 
Posts: n/a
#1: Apr 24 '06
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.'



Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#2: Apr 24 '06

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


Marc Miller wrote:
[color=blue]
> 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?[/color]

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

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


PointedEars
--
This above all: To thine own self be true.
-- William Shakespeare (1564-1616)
Marc Miller
Guest
 
Posts: n/a
#3: Apr 24 '06

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



"Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message
news:6140311.i49f715iq9@PointedEars.de...[color=blue]
> Marc Miller wrote:
>[color=green]
>> 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?[/color]
>
> The difference between an assignment "=" and a comparison "==",
> and a reasonable Subject header.
>
> <URL:http://jibbering.com/faq/>
>
>
> PointedEars[/color]

[color=blue]
> --
> This above all: To thine own self be true.
> -- William Shakespeare (1564-1616)[/color]


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





jshanman
Guest
 
Posts: n/a
#4: Apr 24 '06

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


Marc Miller wrote:[color=blue]
> "Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message
> news:6140311.i49f715iq9@PointedEars.de...[color=green]
> > Marc Miller wrote:
> >[color=darkred]
> >> 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?[/color]
> >
> > The difference between an assignment "=" and a comparison "==",
> > and a reasonable Subject header.
> >
> > <URL:http://jibbering.com/faq/>
> >
> >
> > PointedEars[/color]
>
>[color=green]
> > --
> > This above all: To thine own self be true.
> > -- William Shakespeare (1564-1616)[/color]
>
>
> 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[/color]

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

Randy Webb
Guest
 
Posts: n/a
#5: Apr 24 '06

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


Marc Miller said the following on 4/24/2006 1:41 PM:[color=blue]
> 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")[/color]

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';
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Marc Miller
Guest
 
Posts: n/a
#6: Apr 24 '06

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



"jshanman" <unchartedweb@sbcglobal.net> wrote in message
news:1145903760.315751.97490@v46g2000cwv.googlegro ups.com...[color=blue]
> Marc Miller wrote:[color=green]
>> "Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message
>> news:6140311.i49f715iq9@PointedEars.de...[color=darkred]
>> > Marc Miller wrote:
>> >
>> >> 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[/color]
>>
>>[color=darkred]
>> > --
>> > This above all: To thine own self be true.
>> > -- William Shakespeare (1564-1616)[/color]
>>
>>
>> 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[/color]
>
> 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
>[/color]


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


Randy Webb
Guest
 
Posts: n/a
#7: Apr 24 '06

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


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

<snip>
[color=blue]
>
> 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:[/color]

It does. The .src property of an img object is always a fully qualified
path when your read its .src property.
[color=blue]
> 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";
> }[/color]

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 :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Randy Webb
Guest
 
Posts: n/a
#8: Apr 24 '06

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


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

<snip>
[color=blue]
> 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.[/color]

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';
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Marc Miller
Guest
 
Posts: n/a
#9: Apr 24 '06

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


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 8-))


Dr John Stockton
Guest
 
Posts: n/a
#10: Apr 25 '06

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


JRS: In article <6140311.i49f715iq9@PointedEars.de>, dated Mon, 24 Apr
2006 20:06:23 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <PointedEars@web.de> posted :[color=blue]
>
>
>PointedEars
>--
>This above all: To thine own self be true.
> -- William Shakespeare (1564-1616)[/color]


In your case, that's bad advice.

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

--
© John Stockton, Surrey, UK. yyww merlyn demon co uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Closed Thread