Changing the color of div on "enter click"?

Discussion in 'JavaScript' started by misterdh, Oct 4, 2010.

  1. #1
    Hi,

    I am a javascript newbie and wonders how to do this:

    I want to change this:

    .buttons a, .buttons button{
        background-color:#f5f5f5;
    PHP:
    to this

    .buttons a, .buttons button{
        background-color:#000000;
    PHP:
    when enter is clicked when the form field is focued (to make it behave like a button).

    Anyone knows how to do this?

    This is the code for the button BTW:

    <div class="buttons">
        <a href="#" onclick="this.blur();parent.document.forms[0].submit();">
    Search
        </a>
    </div>
    </form>
    
    PHP:
     
    misterdh, Oct 4, 2010 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    
    <div class="buttons">
        <a href="#" onclick="this.style.backgroundColor="#000000";this.blur();parent.document.forms[0].submit();">
    Search
        </a>
    </div>
    </form>
    
    Code (markup):
     
    camjohnson95, Oct 6, 2010 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    alternatively you could use css:
    
            .buttons a:active
            {
                background-color:#000000;
            }
    Code (markup):
     
    camjohnson95, Oct 6, 2010 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    and I may be wrong but I dont think you need the ".buttons button" part... i'm pretty sure it does nothing. Unless there is some browser that assumes a link is a button, and even so there is still no 'button' tag...
     
    camjohnson95, Oct 6, 2010 IP
  5. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Just throwing my 2 cents out there, I would go with the css way of doing this. It's not only easy to implement / change it's also supported by every browser and will work even if the user has javascript disabled
     
    bigrollerdave, Oct 6, 2010 IP