<!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'"
<!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
you can write a function to multiple property
<input type="text" onMouseover= "callme(this)">Mouse over
me!</span>
<script>
function callme(obj){
obj.style.color='red'
obj.style.otherproperty ='value'
.
..... somthing else
}
</script>
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>
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