Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Issues referencing the correct object

    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!! :)

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Try:onmousedown="roll(this.parentNode);"

  • Users who have thanked Logic Ali for this post:

    rico01 (12-10-2010)

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you! That's exactly what I needed.

  • #4
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!
    Last edited by rico01; 12-10-2010 at 01:38 AM.

  • #5
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    Quote Originally Posted by rico01 View Post
    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...colorgenerator

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,195
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    I don't understand how it works in any browser. Well, kind of I do, but...

    Code:
    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
    Code:
    function roll(obj){
        if ( obj.style.backgroundColor == "pink" )
             obj.style.backgroundColor = "#e5e5e5";
        else
            obj.style.backgroundColor = "pink";
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    rico01 (12-10-2010)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,195
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •