/    Sign up×
Community /Pin to ProfileBookmark

Need to get image name for evaluation

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?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CharlesOct 18.2003 — [font=georgia]Your problem is that [font=monospace]document.images.btnFrom.src[/font] returns a string that represents an absolute URL and you are checking against a relative URL. Try using

[font=monospace]if (/images/letter_buttons/btnFrom_on.gif$/.test(document.images.btnFrom.src)) {}[/font][/font]
Copy linkTweet thisAlerts:
@Mr_JOct 18.2003 — Please try the following

[SIZE=1]

<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()">





[/SIZE]
Copy linkTweet thisAlerts:
@TonnyauthorOct 18.2003 — 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.
Copy linkTweet thisAlerts:
@TonnyauthorOct 20.2003 — 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.
×

Success!

Help @Tonny spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...