Reputation:
jQuery's on a method to install the event handlers for the click and mouse hover events for the same element. The problem is that when I just use the hover method, it works but doesn't work with click method.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyInformation</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p id = "p1">This is paragraph 1</p>
<p id = "p2">This is paragraph 2</p>
<p id = "p3">This is paragraph 3</p>
<script>
$(p1).on
({
mouseenter: function ()
{
$(this).css("color","red");
},
mouseleave: function () {
$(this).css("color","black");
}
click: function () {
alert("hello");
}
});
</script>
</body>
</html>
Upvotes: 1
Views: 48
Reputation: 76
Two mistakes in your code
$(p1)
should be $('#p1')
Comma missing after mouseleave function it should be
mouseleave: function () {
$(this).css("color","black");
},
click: function () {
alert("hello");
}
Upvotes: 0
Reputation: 1
You've missed the comma after the mouseleave
function
$('#p1').on
({
mouseenter: function () one mouse enter
{
$(this).css("color","red");
},
mouseleave: function () {
$(this).css("color","black");
},
click: function () {
alert("hello");
}
});
Upvotes: 0
Reputation: 2127
You have wrong selector and some syntax errors, here is fixed version:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyInformation</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p id = "p1">This is paragraph 1</p>
<p id = "p2">This is paragraph 2</p>
<p id = "p3">This is paragraph 3</p>
<script>
$('#p1').on
({
mouseenter: function () {
$(this).css("color","red");
},
mouseleave: function () {
$(this).css("color","black");
},
click: function () {
alert("hello");
}
});
</script>
</body>
</html>
Upvotes: 1