Click to See Complete Forum and Search --> : Need to get image name for evaluation


Tonny
10-18-2003, 04:26 PM
I wish to perform different actions based upon the image displayed, like this:

<script language="JavaScript" type="text/JavaScript">
<!--
function EnterKey() {
var bnt1;
var btn2;
var btn3;
btn1=String(document.images['btnFrom'].src);
btn2=String(document.images['btnSubject'].src);
btn3=String(document.images['btnMessage'].src);
if ((document.images['btnFrom'].src) == 'images/letter_buttons/btnFrom_on.gif') {
(do something...) ;
} else if ((document.images['btnSubject'].src) == "images/letter_buttons/btnSubject_on.gif") {
(do something else...) ;
} else if ((document.images['btnMessage'].src) == "images/letter_buttons/btnMessage_on.gif") {
(default action...) ;
}
}
//-->
</script>

This gives me the error: "document.images['btnFrom'].src is null or not an Object"
I've also tried:

<script language="JavaScript" type="text/JavaScript">
<!--
function EnterKey() {
if ((document.images['btnFrom'].src) == 'images/letter_buttons/btnFrom_on.gif') {
(do something...) ;
} else if ((document.images['btnSubject'].src) == "images/letter_buttons/btnSubject_on.gif") {
(do something else...) ;
} else if ((document.images['btnMessage'].src) == "images/letter_buttons/btnMessage_on.gif") {
(default action...) ;
}
}
//-->
</script>

This gives no error, but nothing happens.

I'm sure the comparison is failing because "document.images['btnFrom'].src" isn't evaluated as a string and "'images/letter_buttons/btnFrom_on.gif'" is a string.

How do I evaluate the object "document.images['btnFrom'].src" as a string to make the comparison?

Charles
10-18-2003, 04:34 PM
Your problem is that document.images.btnFrom.src returns a string that represents an absolute URL and you are checking against a relative URL. Try using

if (/images\/letter_buttons\/btnFrom_on.gif$/.test(document.images.btnFrom.src)) {}

Mr J
10-18-2003, 04:43 PM
Please try the following


<script language="JavaScript" type="text/JavaScript">
<!--
function EnterKey() {
btn1=document.images['btnFrom'].src;
btn2=document.images['btnSubject'].src;
btn3=document.images['btnMessage'].src;
if (btn1.indexOf("btnFrom_on.gif")!= -1) {
alert("1")
}

if (btn2.indexOf("btnSubject_on.gif")!= -1) {
alert("2")
}

if (btn3.indexOf("btnMessage_on.gif")!= -1) {
alert("3")
}

}
//-->
</script>
<img name="btnFrom" src="images/letter_buttons/btnFrom_on.gif" onclick="EnterKey()">
<img name="btnSubject" src="images/letter_buttons/btnSubject_on.gif" onclick="EnterKey()">
<img name="btnMessage" src="images/letter_buttons/btnMessage_on.gif" onclick="EnterKey()">

Tonny
10-18-2003, 05:06 PM
Thanks to Charles and Mr J.

Just got the reply as I was shutting down for the day, but wanted you to know the replies came through. I won't be testing until Monday, but will let you know the results.

Tonny
10-20-2003, 10:31 AM
I want to thank both Charles and Mr J for their quick and helpful responses.
I'm sorry I did not make my need clearer. The function EnterKey() is called from a separate button from btnFrom, btnSubject, and btnMessage, so Charles' solution was what I needed.
Thanks for this help. I learned something from both solutions.