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 Traversing Filtering - slice() Method</title> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <style> body { font-family: Arial, sans-serif; background: #f9f9f9; padding: 30px; } h2 { color: #333; margin-bottom: 10px; } .description { margin-bottom: 20px; color: #666; } .btn { display: inline-block; padding: 10px 20px; margin: 10px 10px 20px 0; font-size: 16px; font-weight: 600; color: #fff; background-color: #8e44ad; border: 2px solid #71368a; border-radius: 4px; cursor: pointer; text-decoration: none; transition: background-color 0.2s, border-color 0.2s; } .btn:hover { background-color: #71368a; border-color: #5e2c6f; } .item-list { border: 2px solid #999; padding: 15px; background: #fff; width: 400px; } .item { margin: 5px 0; padding: 10px; background: #e8f6f3; border: 1px solid #76d7c4; } .highlight { background: #ffebcc; border-color: #f5b041; } </style> </head> <body> <h2>jQuery Traversing Filtering - slice() Method</h2> <p class="description"> Click a button below to highlight a slice of items using the <kbd>slice()</kbd> method. </p> <button id="slice-range" class="btn">Highlight Items 2 to 4</button> <button id="slice-start" class="btn">Highlight From Item 3 Onward</button> <div class="item-list"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> <div class="item">Item 4</div> <div class="item">Item 5</div> </div> <script> $(function () { // Highlight slice from index 1 to 4 (2nd to 4th item) $('#slice-range').on('click', function () { $('.item').removeClass('highlight'); $('.item').slice(1, 4).addClass('highlight'); }); // Highlight slice starting from index 2 (3rd item onward) $('#slice-start').on('click', function () { $('.item').removeClass('highlight'); $('.item').slice(2).addClass('highlight'); }); }); </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