Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the 'text-decoration' property with jQuery?
To change the text-decoration property with jQuery, use the jQuery css() method. This method allows you to dynamically modify CSS properties of HTML elements, including text decoration styles like underline, overline, line-through, or none.
Syntax
The basic syntax to change text-decoration property is ?
$(selector).css("text-decoration", "value");
Where value can be underline, overline, line-through, or none.
Example
You can try to run the following code to change text-decoration property from underline to overline on mouse hover ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").on({
mouseenter: function(){
$(this).css({"text-decoration": "overline"});
},
mouseleave: function(){
$(this).css({"text-decoration": "underline"});
}
});
});
</script>
<style>
p {
text-decoration: underline;
cursor: pointer;
padding: 10px;
}
</style>
</head>
<body>
<p>Move the mouse pointer on this text to change from underline to overline.</p>
<p>This paragraph also has the same hover effect applied.</p>
</body>
</html>
Multiple Text Decoration Values
You can also set multiple text decoration values or remove text decoration entirely ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").css("text-decoration", "line-through");
});
$("#btn2").click(function(){
$("p").css("text-decoration", "none");
});
$("#btn3").click(function(){
$("p").css("text-decoration", "underline overline");
});
});
</script>
</head>
<body>
<p style="text-decoration: underline;">Click the buttons to change text decoration.</p>
<button id="btn1">Line Through</button>
<button id="btn2">Remove Decoration</button>
<button id="btn3">Underline + Overline</button>
</body>
</html>
Conclusion
The jQuery css() method provides a simple way to dynamically change the text-decoration property. You can apply various decoration styles like underline, overline, line-through, or remove decorations entirely using interactive events.
