-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jquery Notes.js
228 lines (160 loc) · 4.88 KB
/
Jquery Notes.js
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
----------------------Jquery selectors------------------------
######### Basic selectors
$("elem1,elem2,elem3")
$("elem")
$('li')[0] or $('li').get(0)
$($('li')[0]).fadeOut();
########## Adding to the selection
$("elem").add($('anotherelem'))
######### Select by Atrribute
$("li[data-chosen]").fadeOut(); //selector attr only
$("li[data-chosen=nipuna]").fadeOut();//selector attr value
$("[data-chosen]").fadeOut(); //attr only
######### Wildcard selectors
$("li[data-chosen*=23]").fadeOut(); //contains
$("li[data-chosen^=a]").fadeOut(); //starts with
$("li[data-chosen$=23]").fadeOut(); //end with
######### By position
$("li:first").fadeOut();
$("li:last").fadeOut();
$("li:nth(2)").fadeOut();
########### Filter
$(':button')
$(':checkbox')
$(':password')
$(':submit')
=============
$('contains()')
$('selector:not(selector)')
########### JSON Format
{"firstName":"John", "lastName":"Doe"}
------------------ Chaining -----------------------
$('div').hide().show().fadeOut();
------------ Manipulating DOM -------------------
===== Inside
$('div').html("Nipuna<br>");
$('div').text("Nipuna"); //Only text
$("<li>nipuna</li>").appendTo('div')
$("<li>nipuna</li>").prependTo('div')
$('div').append("<li>this is after</li>")
$('div').prepend("<li>this is after</li>")
===== Outside
$("<li>nipuna</li>").insertAfter('div')
$("<li>nipuna</li>").insertBefore('div')
$('div').after("<li>this is after</li>")
$('div').before("<li>this is after</li>")
===== Copying
$(selector).clone().appendTo(anotherSelector);
===== Create an Element
$('<div>',{ <-------- here put just the opening tag
'html':"haha haha",
'class':"mydiv",
.... etc ...
}).appendTo("body");
===== Replacing
$(selector).replaceAll('<li>Nipuna<li>');
$(selector).replaceWith('<li>Nipuna<li>');
===== Removing
$(selector).remove(); //removing from DOM
$(selector).detach(); //detach for editing, and then appennd
$(selector).empty(); // clear inside
======== Wrapping
### before
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>
### Jquery
$( ".inner" ).wrap( "<div class='new'></div>" ); //or
$( ".inner" ).wrapAll( "<div class='new'></div>" );
### after
<div class="container">
<div class="new">
<div class="inner">Hello</div>
</div>
<div class="new">
<div class="inner">Goodbye</div>
</div>
</div>
======== Modify Attributes
$("selector").attr("name","value");
$("selector").prop("name","value");
$("selector").removeAttr("name");
$("selector").removeProp("name");
======= CSS
$(elem).css('style','value'); //or JSON Format
======= Class manipulation
.hasClass() //boolean
.addClass("class1 ........")
.removeClass("class");
.toggleClass("class1 .........");
======= Size
.height()
.width()
======= Positioning
//from the offset parent
.position() // returns JSON {top: 50, left: 50}
.position({top: 100, left: 100}) //sets the position
//from the document
.offset()
.offset({top: 100, left: 100})
-------------------- Event Handling ---------------------
############# on()
e.stopPropagation() //use this to stop recursing through the DOM
//example
$('div').on('click',function(e){ //e is a Event Object
console.log("Clicked");
e.stopPropagation(); //recursing stoped
})
############# off()
$('div').off() //turn off all events
$('div').off('click') //turn off all events
############ Event Object
//log the object & see the data it has
.currentTarget //currently recursing element
.type //event type
.which //with key or button pressed
.timestamp //absolute time event occured
.target // originally clicked element
.data
######### Advanced on()
$(elem).on(event,selector,data,handler)
//selector: to select decendants. so container is binded, but children are clickable.
//data: put {json} and it will be in the Event Object
######### trigger()
$(elem).trigger('click'); //event is programatically occured
-------------------- Browser Event Handling ---------------------
.error(function(){})
.resize()
.scroll() //window or a scrollable element
######### Throwing an error
throw "Fuck!"; //a JS thing, not JQ
######### Form Events
.change()
.focus() --- .blur() //right at it
.focusin() ---- .focusout() //right at it OR A CHILD
.select() //if user selects something in a NESTED TEXTBOX
.submit() // if something NESTED is submitted
######### Keyboard Events
.keydown() //any key
.keypress() //only legit keystrokes, ctrl alt etc don't work
.keyup() // any key
***there you use that "which" data
------------------------------ Effects ----------------------
show()
hide()
toggle()
fadeIn()
fadeOut()
fadeToggle()
fadeTo() // to a opacity level
**** stop() //stop animation
slideUp() //collapse upwards
slideDown() //re-shows the element downwards
------------------------
animate()
//example
$(elem).animate({'height':'700px'},5000); //or {duration:5000}
---------- Tricks ----------------------------------
#get the value from a dropdown
$(selector).val();