views:

199

answers:

3

I want to add an onmouseover event to the submit button of all forms on my drupal site.

How can I do this?

Ben

A: 

With jQuery:

$(document).ready(function(){
    $('input[type="submit"]').hover(
      function(){ 
        // Do something with $(this)
      }
    );
});

EDIT: Updated with another possible solution since you just need to update the background color.

If you don't care about IE6 support, you can also use the :hover CSS pseudo selector for your submit buttons and don't need any javascript. Make sure all your submit buttons have a class set to them (I'm using 'submit' as the class name in this example).

.submit {
  background-color: #ddd;
}
.submit:hover {
  background-color: #f00;
}
Shawn Steward
Just to clarify, the above can be done either in the theme or in a custom module. If using the custom module route (which may or may not make sense depending on what's being accomplished), just use hook_init to add some JS using drupal_add_js() and you're on your way.
Mike Crittenden
Also instead of using $(document).ready it's a good idea to do it the Drupal way and add a behaviour, see here for details: http://api.drupal.org/api/drupal/developer--topics--javascript_startup_guide.html/6#Drupal.behaviors
alxp
I had a look at the link. I'm not sure where I would put the code. I basically am trying to change all my buttons so that onmouseover they all turn red. Can you give me a little bit more explanation please?Thanks.
Ben
See my updated answer for another possible solution using the :hover CSS pseudo selector.
Shawn Steward
Unfortunately I need to take into account ie6 which was why I want to follow a javascript solution.
Ben
A: 

Hi Shawn,

Thanks very much for your answer. I have a custom module already and have put the code in there. However, I am getting an error. The code I have entered is:

function defprofile_init() {
$(document).ready(function(){
$('input[type="submit"]').hover(
function(){
// Do something with $(this)
alert("hi");
}
);
});
}

Do I need to replace the word function with a specific name?

The error I'm getting is:

Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'

Ben

Ben
The $(document).ready should not be inside a function, it needs to be out on it's own.
Shawn Steward
A: 

I have managed to partly resolve my problem by adding the following code to a custom module:

function defprofile_form_alter(&$form, &$form_state, $form_id) {
    $form['submit']['#attributes'] = array('onMouseOver' => "this.style.backgroundColor='#cc0000'",'onMouseOut' => "this.style.backgroundColor='#000'"); }

However, this only works for submit buttons, and I want it to work for all buttons. I've tried replacing 'submit' with 'button' but then the code stops working altogether.

Ben