<!DOCTYPE html>
<html>
<body>
<input type="text" onMouseover= "this.style.color='red'">Mouse over
me!</input>
</body>
</html>
I would also like to call "this.style.cursor='default'" along with "this.style.color='red'"
;<input type="text" onMouseover= "function1(); function2();">
Mouse over me!
</input>
<input type="text" onMouseover="dedicatedFunction()">
Mouse over me!
</input>
and define this function in a <script> tag:
function dedicatedFunction() {
function1()
function2()
}
As Xufox said, you can also use addEventListener to do this:
This means that you have access to your DOM node directly as a Javascript object, by using a DOM Selector:
var node = document.getElementById('yourObjectId')
or directly by creating the DOM node via Javascript:
var node = document.createElement('input')
You can then use addEventListener on your object:
node.addEventListener('mouseover', function1)
node.addEventListener('mouseover', function2)
Or even directly use anonymous functions
node.addEventListener('mouseover', function () {
// ...
})
The benefits of this way is that you will be able to add event listeners any time you want. You will also be able to remove an eventListener any time you want by using removeEventListener
https://developer.mozilla.org/fr/docs/Web/API/EventTarget/removeEventListener
addEventListener (the better way).addEventListener will be better when doing everything programmatically, but in this case the declarative way is the easiest way (imo).:hover is different from a single mouseover in that mouseover can make a change persistent whereas :hover can’t.you can simply use ; to separate two events:
<!DOCTYPE html>
<html>
<body>
<input type="text" onmouseover= "this.style.color='red';this.style.cursor='default';">Mouse over me!
</body>
</html>
But You'd better use <script> to write standard code:
<!DOCTYPE html>
<html>
<body>
<input type="text" onmouseover="foo(this);">Mouse over me!
<script type="text/javascript">
function foo(ele) {
ele.style.color="red";
ele.style.cursor="default";
}
</script>
</body>
</html>
using addEventListener is the best way, but it may cause compatibility question among browsers, you can read it for reference:
addeventlistener-vs-onclick
No need of using Javascript events here just for changing the color and cursor style on hover of an element.
You can use :hover class of CSS.
span:hover {
cursor: default;
color: red;
}
/* Override */
#input {
color: gray !important;
}
<span>
<input id="input" type="text" onMouseover= "this.style.color='red'">Mouse over me!
</span>
onMouseover events:hover is different from a single mouseover in that mouseover can make a change persistent whereas :hover can’t.mouseover, the color will stay red. If you use :hover the color will only be red when the element is actually hovered. It’s not the same. It would be the same if the OP used mouseover and mouseout to reverse the effects.
addEventListenerinstead.inputelement with</span>in your real code.inputelement isn't open (it can't be, it's a void element). But yeah, there's definitely a dangling</span>there.closein quotes.