/    Sign up×
Community /Pin to ProfileBookmark

Don’t execute if property not supported?

I’ve written a little function for the mouseover so that a button can change colour in IE. Only thing is, Mozilla doesn’t recognise the style property and gives a javascript error. How do I get my code to not execute if the property is not supported? The if statement that I’m using isn’t working.

[code]
<input name=”target1″ class=”myClass” type=”submit” onMouseOver=”doMouseOver(‘target1’);” value=”hello”/>
[/code]

[code]
function doMouseOver(srcName)
{
srcObj=document.getElementById(srcName);
if (srcObj.style) {
srcObj.style.backgroundColor = “#ffcc00”;
}
}
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ccamaratJan 19.2005 — [URL=http://www.xs4all.nl/~peterned/csshover.html#]This article may help[/URL]. Most modern browsers (including FireFox and Mozilla) support adding ":hover" to the style of any tag. In this case your style code would look something like this:&lt;style&gt;
.myClass:hover {
background-color: #ffcc00;
}
&lt;/style&gt;
IE will ignore it so you'll have to rewrite the javascript to be IE only.

Incidentally, I don't think your problem is with Style recognition; I think it has more to do with your assignment of srcObj through getElementById. If you take advantage of JavaScript's "this" object your mouseover problem disappears (well, in FireFox anyway).&lt;script language=javascript&gt;
function doMouseOver(srcObj)
{
srcObj.style.backgroundColor = "#ffcc00";
}
&lt;/script&gt;
...
&lt;input name="target1" class="myClass" type="submit" onMouseOver="doMouseOver(this);" value="hello"/&gt;
Copy linkTweet thisAlerts:
@PittimannJan 19.2005 — Hi!

I was just about to post something similar, using 'this'. I deleted that part after previewing my post and seeing yours, ccamarat.

The error in Pingu's code is simply based on the fact, that the button has no 'id', only a 'name'.

Cheers - Pit
Copy linkTweet thisAlerts:
@ccamaratJan 19.2005 — "Must remember to pay attention to detail" - d'Oh! :p
Copy linkTweet thisAlerts:
@PittimannJan 19.2005 — [i]Originally posted by ccamarat [/i]

[B]"Must remember to pay attention to detail" - d'Oh! :p [/B][/QUOTE]
Yeees! ?
Copy linkTweet thisAlerts:
@PinguauthorJan 19.2005 — Thank you, that worked!

Where can I find a good guide to JavaScript so I can work these things out for myself?
Copy linkTweet thisAlerts:
@ccamaratJan 20.2005 — I mostly use [URL=http://www.google.com]Google[/URL] because most of my questions have already been answered. Wrox publishing has books on most everything; they have helped me immensely (probably because most of the Beginning series assumes no knowledge of anything). They’re the thick red books in the programming section of your local Border’s. Another trick I like doing is "View Source". Break it - then figure out why it doesn't work (my dad used to hate it when I did that to his toys! :p)
×

Success!

Help @Pingu 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...