-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 04 codes.txt
69 lines (51 loc) · 1.75 KB
/
Chapter 04 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
let counter = 0;
for (let i = 0; i < 10; i++) {
counter++;
const $span = $(`<span>Hello :${counter}</span>`);
$span.text('New');
$('.div2').append($span);
}
-----------------------------------------------
for (let i = 0; i < 3; i++) {
const $btn = $(`<button>After ${i + 1}</button>`);
$('.div4').after($btn);
$btn.click(function () {
console.log($(this).text());
});
$(`<button>Before ${i + 1}</button>`).insertBefore('.div4').click(function () {
console.log($(this).text());
});
}
-----------------------------------------------------
$('#btn1').click(() => {
const val = $('input').val();
$('.div1').text(val);
$('.div2').append(val);
$('.div3').prepend(val);
$('.div4').remove();
});
---------------------------------------------------
$('#btn2').click(() => {
counter++;
const $div = $(`<div>Hello :${counter}</div>`);
$('.div3').append($(`<div>Hello :${counter}</div>`));
$(`<div>Hello :${counter}</div>`).appendTo('.div3');
console.log($div);
});
-------------------------------------------------
$('#btn1').click(() => {
// Using .replaceAll() to replace selected elements with new content
$('<h3>Hello</h3>').replaceAll('.div3');
// Using .replaceWith() to replace the entire .div2 with new content
$('.div2').replaceWith('<h3>Hello 3</h3>');
});
--------------------------------------------------
$('#btn2').click(() => {
// Clone the input element with id 'myIn'
const $ele = $('#myIn').clone();
// Log the cloned element to the console
console.log($ele);
// Clone and prepend the last input element to the first div element
$('input').last().clone().prependTo('div');
});
------------------------------------------------