-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 02 codes.txt
300 lines (249 loc) · 7.5 KB
/
Chapter 02 codes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Select by tag
$(“p”) // Selects all <p> elements
// Select by class
$(“.highlight”) // Selects elements with class “highlight”
// Select by ID
$(“#header”) // Selects the element with ID “header”
------------------------------------------------------------------
HTML:
<!DOCTYPE html>
<html>
<head><title>jQuery Course</title></head>
<body>
<div>Hello World 1</div>
<div class=”div2”>Hello World 2</div>
<div id=”div3”>Hello World 3</div>
<script
src=”https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.m in.js”></script>
<script src=”app2.js”></script>
</body>
</html>
--------------------------------------------------------------------------
APP2.JS
console.log($(‘div’));
$(‘div’).html(‘Hello’);
console.log($(‘.div2’));
console.log($(‘#div3’));
console.log($(‘#div3, .div2’));
const $ele1 = $(‘div’);
console.log($ele1);
$ele1.html(‘Hello 2’);
const $ele2 = $(‘.div2’);
console.log($ele2);
const $ele3 = $(‘#div3’);
console.log($ele3);
const $ele4 = $(‘#div3, .div2’);
console.log($ele4);
-----------------------------------------------------------------------------
// Select by attribute
$(“input[type=’text’]”) // Selects all text input elements
// Select with specific attribute
$(“[data-toggle]”) // Selects elements with “data-toggle” attribute
Exercise: Select using the attribute:
HTML:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8”>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>
<title>Attribute Selector Example</title>
<script src=”https://code.jquery.com/jquery-3.6.0.min.js”></script>
</head>
<body>
<h1>Attribute Selector Example</h1>
<button data-toggle=”modal”>Open Modal</button>
<button data-toggle=”sidebar”>Open Sidebar</button>
<script src=”script.js”></script>
</body>
</html>
JavaScript (script.js):
$(document).ready(function() {
// Attach a click event to elements with data-toggle attribute
$(“[data-toggle]”).click(function() {
const target = $(this).data(“toggle”);
alert(`Toggle ${target}`);
});
});
---------------------------------------------------------------
// Select the first <li> element inside <ul>
$(“ul li:first-child”)
// Select odd-numbered <tr> elements in a table
$(“table tr:nth-child(odd)”)
Exercise: Using jQuery, select the first list item and change its background color to blue:
HTML:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8”>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>
<title>Change CSS with jQuery</title>
<script src=”https://code.jquery.com/jquery-3.6.0.min.js”></script>
</head>
<body>
<h1>Change CSS with jQuery</h1>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script src=”script.js”></script>
</body>
</html>
JavaScript (script.js):
$(document).ready(function() {
// Change CSS for the first list item
$(“ul li:first-child”).css(“color”, “blue”);
});
--------------------------------------------------------------
HTML:
<!DOCTYPE html>
<html>
<head><title>jQuery Course</title>
<style>
.box{
width:200px;
border:5px solid #ddd;
padding:20px;
margin:10px;
}
</style>
</head>
<body><nav class=”main”>
<div class=”main”>
<div xclass=”div1”>Hello World 1</div>
<div class=”div2”><span>Hello World 2</span></div> <div class=”div3” id=”div3”>Hello World 3</div> <div class=”div4”>Laurence Svekis</div>
<input >
<div class=”main”>
<button id=”btn1”>1</button>
<button id=”btn2”>2</button>
</div>
<button id=”btn1”>3</button>
</div></nav>
<button id=”btn1”>4</button>
<script
src=”https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.m in.js”></script>
<script src=”app14.js”></script>
</body>
</html>
app14.js
$(‘#btn1’).click(function(){
console.log($(this).parent().length);
console.log($(this).parents().length);
console.log($(this).parents());
console.log($(this).parents(‘.main’));
console.log($(this).parentsUntil(‘nav’));
})
$(‘#btn2’).click(function(){
$(this).parents(‘.main’).each(function(){
$(this).addClass(‘parentM’);
})
})
-------------------------------------------------------
// Filter by even-numbered elements
$(“li”).filter(“:even”)
// Filter by elements containing specific text
$(“p”).filter(“:contains(‘jQuery’)”)
--------------------------------------------------------
HTML:
<!DOCTYPE html>
<html>
<head>
<title>jQuery Course</title>
<style>
.box {
border: 1px solid #ddd;
padding: 5px;
color: red;
}
.box1 {
border: 1px solid #ddd;
padding: 5px;
color: blue;
}
.box2 {
border: 1px solid #ddd;
padding: 5px;
color: green;
}
</style>
</head>
<body>
<nav>
<div class=”main”>
<div class=”div1”>Hello World 1</div>
<div class=”div2”><span>Hello World 2</span></div> <div class=”div3” id=”div3”>Hello World 3</div> <div class=”div4”>Laurence Svekis</div>
<input>
<button id=”btn1”>1</button>
<button id=”btn2”>2</button>
<button id=”btn3”>3</button>
<span>Hello</span>
</div>
</nav>
<button id=”btn4”>4</button>
<button id=”btn5”>5</button>
<script
src=”https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.m in.js”></script>
<script src=”app16.js”></script>
</body>
</html>
----------------------------------------------------------------
app16.js
$(‘#btn1’).click(()=>{
console.log($(‘.main’).children());
$main = $(‘.main’).children();
$main.first().css(‘color’,’red’);
$main.last().css(‘color’,’blue’);
$main.eq(7).css(‘color’,’green’);
$main.eq(6).css(‘background-color’,’green’);
})
$(‘#btn2’).click(()=>{
$main = $(‘.main’).children();
$btns = $main.filter(‘button’);
$main.filter(‘button’).css(‘background-color’,’black’); $btns.css(‘color’,’
white’);
})
$(‘#btn3’).click(()=>{
$main = $(‘.main’).children();
$btns = $main.not(‘button’);
$main.not(‘button’).css(‘background-color’,’purple’); $btns.css(‘color’,’
white’);
})
$(‘#btn4’).click(()=>{
$(‘.main div’).not(‘.div1’).css(‘color’,’red’); })
$(‘#btn5’).click(()=>{
$(‘.main div’).filter(‘div’).css(‘color’,’green’); })
-------------------------------------------------------
// Chain methods to select and manipulate an element
$(“#myElement”).addClass(“highlight”).slideUp(1000);
Exercise: How to chain multiple methods together:
HTML:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8”>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>
<title>Chaining Methods with jQuery</title>
<script src=”https://code.jquery.com/jquery-3.6.0.min.js”></script>
</head>
<body>
<h1>Chaining Methods with jQuery</h1>
<div id=”box”></div>
<button id=”animateButton”>Animate</button>
<script src=”script.js”></script>
</body>
</html>
JavaScript (script.js):
$(document).ready(function() {
// Attach click event to the animate button
$(“#animateButton”).click(function() {
// Chain methods to animate the box
$(“#box”)
.css(“background-color”, “blue”)
.animate({ width: “200px” }, 1000)
.delay(500)
.fadeOut()
.fadeIn()
.slideUp();
});
});
-----------------------------------------------