...

View Full Version : Issues referencing the correct object



rico01
12-09-2010, 08:18 PM
Hi there. I have a table which I would like to be highlighted with the click of a button, but I can't seem to reference it correctly. I can make the <td> clickable and function, but when I try to apply it to the button I can't make it reference the td cell, rather than change the background color of the button.

The function is:

function roll(obj){

obj.style.backgroundColor == "pink" ? obj.style.backgroundColor = "#e5e5e5" :
obj.style.backgroundColor = "pink";

}

Html:

<td style="background-color:#e5e5e5;"><img onmousedown="roll(this);" src="images/plus.png" title="Highlight Lot" /></td>


Thanks a lot!! :)

Logic Ali
12-09-2010, 08:37 PM
Try:onmousedown="roll(this.parentNode);"

rico01
12-09-2010, 09:01 PM
Thank you! That's exactly what I needed. :)

rico01
12-10-2010, 01:33 AM
Hey does anyone have any idea why this code doesn't work in safari? The cell will change to the new color, but then not change back again. Some colours seem to work, others don't. So strange.

Thanks!

gizmo1650
12-10-2010, 01:44 AM
Hey does anyone have any idea why this code doesn't work in safari? The cell will change to the new color, but then not change back again. Some colours seem to work, others don't. So strange.

Thanks!

My guess would be you are using a color Safari doesn't know, try using hexadecimal instead of plaintext for the colors.
you can get the hex code here http://www.2createawebsite.com/build/hex-colors.html#colorgenerator

Old Pedant
12-10-2010, 01:44 AM
I don't understand how it works in any browser. Well, kind of I do, but...



function roll(obj){
obj.style.backgroundColor = ( obj.style.backgroundColor == "pink" ) ? "#e5e5e5" : "pink";
}

Try that, instead.

Don't use the ternary operator unless you are *assigning* its value to something. Your code could also have been more simply written as just


function roll(obj){
if ( obj.style.backgroundColor == "pink" )
obj.style.backgroundColor = "#e5e5e5";
else
obj.style.backgroundColor = "pink";
}

Old Pedant
12-10-2010, 01:46 AM
Ahhh...Gizmo's probably right! "pink" is not an official color. Most browsers support it, but not surprising that Apple would do something different.

Still, use one or the other version of the code I show. That use of the ternary operator is just weird.

rico01
12-10-2010, 02:02 AM
Thank you gizmo1650 for that website, very handy! Also @ Old Pedant, thank you very much for showing me a more appropriate way of writing the function. Much appreciated!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum