Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript onmouseover & onmouseout not working to disable button [duplicate]

I'm trying this piece of code to achieve a button that gets disabled on mouse hover and gets enabled on mouse out.

<input type="button" value="submit" onMouseOver="this.disabled = true;" onMouseOut="this.disabled = false;" />

But it doesn't seem to work. It disables the button on mouse over but doesn't enable it on mouse out. Any suggestions? Thanks!

like image 819
The High Guy Avatar asked Jun 30 '26 13:06

The High Guy


2 Answers

Disabled elements do not fire events. You can place a element over top of the element and listen to the event fired on that element.

like image 105
Oleksandr T. Avatar answered Jul 03 '26 01:07

Oleksandr T.


One would set the mouseout to disabled = '' instead.

<input type="button" name="test" id="test" value="roll over me" onmouseover="this.disabled=true;" onmouseout="this.disabled='';">

The disabled property only looks to see if it's there at all. One can set disabled='anything' and it will be disabled. Furthermore, add padding to the SPAN tag and will allow the events to work properly. Without padding, it will not trap the events because the input button is disabled. In the code below, a green background is added to display the SPAN area.

<span style="padding: 8px; background: green;"  onmouseout="this.firstChild.disabled='';"><input type="button" name="test" id="test" value="roll over me" onmouseover="this.disabled=true;"></span>

Hope this helps!

Source: "Javascript: enable/disable button with mouseover/mouseout"

like image 25
Kevin Austin Avatar answered Jul 03 '26 02:07

Kevin Austin