473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

simple function not working in FF

Incredibly, this function works as expected in IE (changing the
div_id's color after every click) , whereas it only changes div_id's
color once
function colorDiv(div_id)
{
if (document.getElementById(div_id).style.backgroundC olor ==
'#00cc00'){
document.getElementById(div_id).style.backgroundCo lor = '#00cccc';

}else{
document.getElementById(div_id).style.backgroundCo lor = '#00cc00';
}

}

I call the above function this way:

<div id="div_1" onclick="colorDiv('div_1'); return false;" >
<div id="div_2" onclick="colorDiv('div_2'); return false;" >
<div id="div_3" onclick="colorDiv('div_3'); return false;" >

What's wrong with that?

reagards - julian

May 18 '06 #1
9 1402
My guess is that in this check:
if (document.getElementById(div_id).style.backgroundC olor == '#00cc00')

It is changing the cc to CC which then makes the statement incorrect.
I would try to .toLowerCase() the backgroundColor like:
if (document.getElementById(div_id).style.backgroundC olor.toLowerCase()
== '#00cc00')

May 18 '06 #2

ma**********@gmail.com wrote:
My guess is that in this check:
if (document.getElementById(div_id).style.backgroundC olor == '#00cc00')

It is changing the cc to CC which then makes the statement incorrect.
I would try to .toLowerCase() the backgroundColor like:
if (document.getElementById(div_id).style.backgroundC olor.toLowerCase()
== '#00cc00')

It doesn't work either.

I tryied with 'rgb(x,x,x )' without luck.

if (document.getElementById(div_id).style.backgroundC olor == 'rgb(255,
155,60)'){
document.getElementById(div_id).style.backgroundCo lor = 'rgb(55,
155,240)';
}else{
document.getElementById(div_id).style.backgroundCo lor = 'rgb(255,
155,60)';
}

It's strange isn't it?

regards - julian

May 18 '06 #3
julian_m wrote:
ma**********@gmail.com wrote:
My guess is that in this check:
if (document.getElementById(div_id).style.backgroundC olor == '#00cc00')


It is changing the cc to CC which then makes the statement incorrect.
I would try to .toLowerCase() the backgroundColor like:
if (document.getElementById(div_id).style.backgroundC olor.toLowerCase()
== '#00cc00')


It doesn't work either.

I tryied with 'rgb(x,x,x )' without luck.

if (document.getElementById(div_id).style.backgroundC olor == 'rgb(255,
155,60)'){
document.getElementById(div_id).style.backgroundCo lor = 'rgb(55,
155,240)';
}else{
document.getElementById(div_id).style.backgroundCo lor = 'rgb(255,
155,60)';
}

It's strange isn't it?

No, one or other (I can remember which one) browser inserts a space
after the comma. You have to use a regular expression replace to
normalise the value before the comparison.

--
Ian Collins.
May 18 '06 #4
Ok,
I have come across this before and had to go back to old code to see
how I fixed it.
Here is what you need to do.
You were on the right track with the rgb values.
Here is my code.
obviously different colors but the same gist...

var background = element.style.backgroundColor.replace(/ /g, "");
if(background == 'rgb(254,147,17)') {
element.style.color = '#333333';
element.style.backgroundColor= '#FFFFFF';
} else {
element.style.color = '#FFFFFF';
element.style.backgroundColor= 'rgb(254,147,17)';
}

May 18 '06 #5
julian_m wrote:
function colorDiv(div_id)
{ alert(document.getElementById(div_id).style.backgr oundColor) would be a
good thing to do here. if (document.getElementById(div_id).style.backgroundC olor ==
'#00cc00'){
document.getElementById(div_id).style.backgroundCo lor = '#00cccc';

}else{
document.getElementById(div_id).style.backgroundCo lor = '#00cc00';
}

}

What's wrong with that?

Firefox is converting your hexcodes to rgb values. So you should be
checking to see that backgroundColor is equal to some rgb value. But
note the format in the alert -- rgb(0, 204,0) is not the same as rgb(0,
204, 0).

May 18 '06 #6
ASM
ma**********@gmail.com a écrit :
Ok,
I have come across this before and had to go back to old code to see
how I fixed it.

simpliest way is to use class names
because each browser has its own way to remenber colors

--
Stephane Moriaux et son [moins] vieux Mac
May 18 '06 #7

ma**********@gmail.com wrote:
Here is what you need to do.
You were on the right track with the rgb values.
Here is my code.
obviously different colors but the same gist...

var background = element.style.backgroundColor.replace(/ /g, "");
if(background == 'rgb(254,147,17)') {
element.style.color = '#333333';
element.style.backgroundColor= '#FFFFFF';
} else {
element.style.color = '#FFFFFF';
element.style.backgroundColor= 'rgb(254,147,17)';
}


It works like a charm ;)
Thanks for your time !!!

regards - julian

May 18 '06 #8
ASM
julian_m a écrit :
ma**********@gmail.com wrote:
var background = element.style.backgroundColor.replace(/ /g, "");
if(background == 'rgb(254,147,17)') {
element.style.color = '#333333';
element.style.backgroundColor= '#FFFFFF';
} else {
element.style.color = '#FFFFFF';
element.style.backgroundColor= 'rgb(254,147,17)';
}

It works like a charm ;)
Thanks for your time !!!


Sorry, it doesn't work with my iCab nor my Opera

This bellow and much more simple works fine everywhere.

<style type="text/css" media="screen">
div { line-height:2em; border:1px solid blue; margin:9px;
cursor:pointer; text-align:center; color:#333;}
..one { background: rgb(254,147,17); color: #fff; }
..two { background: #fff; color: #333;}
</style>
<script type="text/javascript">
function colorDiv(div_id) {
var element = document.getElementById(div_id);
element.className = (element.className=='one')?
'two' : 'one' ;
}
</script>
</head>
<body>
<div id="div_1" onclick="colorDiv('div_1');"> test 1 </div>
<div id="div_2" onclick="colorDiv('div_2');"> test 2 </div>
<div id="div_3" onclick="colorDiv('div_3');"> test 3 </div>

--
Stephane Moriaux et son [moins] vieux Mac
May 19 '06 #9
JRS: In article <11**********************@j33g2000cwa.googlegroups .com>
, dated Thu, 18 May 2006 12:36:46 remote, seen in
news:comp.lang.javascript, julian_m <ju***********@gmail.com> posted :
Incredibly, this function works as expected in IE (changing the
div_id's color after every click) , whereas it only changes div_id's
color once
function colorDiv(div_id)
{
if (document.getElementById(div_id).style.backgroundC olor ==
'#00cc00'){
document.getElementById(div_id).style.backgroundCo lor = '#00cccc';

}else{
document.getElementById(div_id).style.backgroundCo lor = '#00cc00';
}

}

I call the above function this way:

<div id="div_1" onclick="colorDiv('div_1'); return false;" >
<div id="div_2" onclick="colorDiv('div_2'); return false;" >
<div id="div_3" onclick="colorDiv('div_3'); return false;" >

What's wrong with that?


Your lines are too long, so your sending agent has wrapped one. Write
shorter lines, and don't use tabs in News - see FAQ 2.3.

The expression document.getElementById(div_id) is long & slow, and
should be factored out as X ; indeed, maybe X.style can be factored.
function colorDiv(div_id) { var X = document.getElementById(div_id)
if (X.style.backgroundColor == '#00cc00')
X.style.backgroundColor = '#00cccc';
else
X.style.backgroundColor = '#00cc00';
}
function colorDiv(div_id) { var X = document.getElementById(div_id)
X.style.backgroundColor =
X.style.backgroundColor == '#00cc00' ? '#00cccc' : '#00cc00' }
Then it is likely that you will have initialised to one of those
colours, in which case you might do

var A = 1 // or 0

function colorDiv(div_id) {
document.getElementById(div_id).style.backgroundCo lor =
(A = !A) ? '#00cccc' : '#00cc00' }
NOTES :
(1) I may have introduces errors in the code; that shown is merely
illustrative of concept.
(2) I have not addressed your original question; see what others have
written for that. But the final version avoids assumptions about the
format given on reading background colour.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bruce W...1 | last post by:
In my effort to learn PHP I'm playing with some simple email scripts. They worked a few days ago but they stopped working. The only thing I've done to this Windows 2000 PC in this time was a...
7
by: NotGiven | last post by:
I have a field called telephone whose ONBLUR action is to call a javascript function: validatePhoneNumber(telephone) The non-working function is: function validatePhoneNumber(v) { var phone...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
7
by: WSPL5 | last post by:
I have done this function before and I cannot figure out why it is not working. I am receiving the results of an xmlhttp call and I am looping through the values I am creating links with an OnClick. ...
2
by: kigoobe | last post by:
I am having a very weird issue here, a simple form submit not working. Trying for several hours now ... must be something stupid ... help appreciated. First, the html - <form id="formFinale"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.