Codelab Editor
Free Online HTML Editor
Run
Learn jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>jQuery toggleClass() with Boolean</title> <style> body { font-family: Arial, sans-serif; padding: 40px; background-color: #f9f9f9; } h1 { color: #333; } p { font-size: 16px; color: #444; } .content-box { padding: 20px; background-color: #fff; border: 1px solid #ccc; border-radius: 6px; margin-top: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); } .box { padding: 15px; background-color: #E3F2FD; border: 2px dashed #2196F3; border-radius: 6px; color: #0D47A1; margin-bottom: 15px; transition: background-color 0.3s, border-color 0.3s, color 0.3s; } .highlighted { background-color: #FFF9C4 !important; border-color: #FFEB3B !important; color: #F57F17 !important; } button { margin-right: 10px; margin-top: 15px; padding: 10px 20px; background-color: #FF9800; color: white; font-size: 14px; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #FB8C00; } </style> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> </head> <body> <h1>jQuery <code>toggleClass()</code> with Boolean Parameter</h1> <p> The <code>toggleClass(className, state)</code> method adds the class if <code>state</code> is <code>true</code>, or removes it if <code>state</code> is <code>false</code>. In this example, clicking either button explicitly sets or clears the <code>highlighted</code> class. </p> <div class="content-box"> <div class="box" id="myBox"> I will be highlighted based on the boolean state. </div> <button id="setTrueBtn">Force Highlight On</button> <button id="setFalseBtn">Force Highlight Off</button> </div> <script> $(document).ready(function() { $('#setTrueBtn').click(function() { $('#myBox').toggleClass('highlighted', true); }); $('#setFalseBtn').click(function() { $('#myBox').toggleClass('highlighted', false); }); }); </script> </body> </html>
Editor Commands
Ctrl
F
Find
Ctrl
G
Find Next
Ctrl
⇧
G
Find Previous
Ctrl
⇧
F
Find & Replace
Ctrl
]
Indent Code Right
Ctrl
[
Indent Code Left
⇧
Tab
Auto Indent Code
Ctrl
/
Line Comment
Ctrl
Alt
/
Block Comment
TutsInsider Actions
Alt
S
Run Code
Ctrl
C
Copy Editor Code
Ctrl
A
Select All Editor Code
Ctrl
S
Save/Download
Ctrl
Alt
X
Rotate
Ctrl
Alt
T
Switch Theme
Ctrl
⇧
C
Open Console
Esc
Close Console