{"id":3384,"date":"2015-06-16T18:06:01","date_gmt":"2015-06-16T12:36:01","guid":{"rendered":"http:\/\/cmscomputer.in\/blog\/?p=3384"},"modified":"2015-07-03T20:21:39","modified_gmt":"2015-07-03T14:51:39","slug":"jquery-selectors","status":"publish","type":"post","link":"https:\/\/www.cmscomputer.in\/blog\/jquery-selectors\/","title":{"rendered":"JQuery Selectors"},"content":{"rendered":"<p>The jQuery function $() is used to find elements on the page to work with.<\/p>\n<h3>Common Selectors<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Selector<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>element<\/td>\n<td>$(&#8220;p&#8221;).hide();<\/td>\n<td>The jQuery element selector selects elements based on the element name.<\/td>\n<\/tr>\n<tr>\n<td>#id<\/td>\n<td>$(&#8220;#test&#8221;).hide();<\/td>\n<td>The #id selector &#8211; Jquery id selector uses the id attribute of an HTML tag to find the specific element.<\/td>\n<\/tr>\n<tr>\n<td>.class<\/td>\n<td>$(&#8220;.test&#8221;).hide();<\/td>\n<td>The .class selector -JQuery class selector finds elements with a specific class.<\/td>\n<\/tr>\n<tr>\n<td>:first<\/td>\n<td>$(&#8220;p:first&#8221;).hide();<\/td>\n<td>The first p element.<\/td>\n<\/tr>\n<tr>\n<td>:last<\/td>\n<td>$(&#8220;p:last&#8221;).hide();<\/td>\n<td>The last p element.<\/td>\n<\/tr>\n<tr>\n<td>:even<\/td>\n<td>$(&#8220;tr:even&#8221;)<\/td>\n<td>All even tr elements<\/td>\n<\/tr>\n<tr>\n<td>:odd<\/td>\n<td>$(&#8220;tr:odd&#8221;)<\/td>\n<td>All odd tr elements.<\/td>\n<\/tr>\n<tr>\n<td>:first-child<\/td>\n<td>$(&#8220;p:first-child&#8221;)<\/td>\n<td>All p elements that are the first child of their parents.<\/td>\n<\/tr>\n<tr>\n<td>:last-child<\/td>\n<td>$(&#8220;p:last-child&#8221;)<\/td>\n<td>All p elements that are the last child of their parents.<\/td>\n<\/tr>\n<tr>\n<td>:nth-child(n)<\/td>\n<td>$(&#8220;p:nth-child(2)&#8221;)<\/td>\n<td>All p elements that are the nth child of their parents.<\/td>\n<\/tr>\n<tr>\n<td>:input<\/td>\n<td>$(&#8220;:input&#8221;)<\/td>\n<td>All input elements.<\/td>\n<\/tr>\n<tr>\n<td>:text<\/td>\n<td>$(&#8220;:text&#8221;)<\/td>\n<td>All input elements with type=&#8221;text&#8221;.<\/td>\n<\/tr>\n<tr>\n<td>:radio<\/td>\n<td>$(&#8220;:radio&#8221;)<\/td>\n<td>All input elements with type=&#8221;radio&#8221;.<\/td>\n<\/tr>\n<tr>\n<td>:checkbox<\/td>\n<td>$(&#8220;:checkbox&#8221;)<\/td>\n<td>All input elements with type=&#8221;checkbox&#8221;.<\/td>\n<\/tr>\n<tr>\n<td>:submit<\/td>\n<td>$(&#8220;:submit&#8221;)<\/td>\n<td>All input elements with type=&#8221;submit&#8221;.<\/td>\n<\/tr>\n<tr>\n<td>:button<\/td>\n<td>$(&#8220;:button&#8221;)<\/td>\n<td>All input elements with type=&#8221;button&#8221;.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Events<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Event<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>$(document).ready()<\/td>\n<td>$(document).ready(function(){<br \/>\n&#8230;<br \/>\n});<\/td>\n<td>Allows us to execute a function when the document is fully loaded. This is to prevent any jQuery code from running before the document is finished loading (is ready).<\/td>\n<\/tr>\n<tr>\n<td>click()<\/td>\n<td>$(&#8220;p&#8221;).click(function(){<br \/>\n$(this).hide();<br \/>\n});<\/td>\n<td>The click() method attaches an event handler function to an HTML element. The function is executed when the user clicks on the HTML element.<\/td>\n<\/tr>\n<tr>\n<td>dblclick()<\/td>\n<td>$(&#8220;p&#8221;).dblclick(function(){<br \/>\n$(this).hide();<br \/>\n});<\/td>\n<td>The dblclick() method attaches an event handler function to an HTML element. The function is executed when the user double-clicks on the HTML element.<\/td>\n<\/tr>\n<tr>\n<td>mouseenter()<\/td>\n<td>$(&#8220;#p1&#8221;).mouseenter(function(){<br \/>\nalert(&#8220;You entered p1!&#8221;);<br \/>\n});<\/td>\n<td>The mouseenter() method attaches an event handler function to an HTML element. The function is executed when the mouse pointer enters the HTML element.<\/td>\n<\/tr>\n<tr>\n<td>mouseleave()<\/td>\n<td>$(&#8220;#p1&#8221;).mouseleave(function(){<br \/>\nalert(&#8220;Bye! You now leave p1!&#8221;);<br \/>\n});<\/td>\n<td>The mouseleave() method attaches an event handler function to an HTML element. The function is executed when the mouse pointer leaves the HTML element.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Effects<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Method<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>hide()<br \/>\nshow()<\/td>\n<td>$(&#8220;#id&#8221;).click(function(){<br \/>\n$(&#8220;p&#8221;).hide();<br \/>\n});<br \/>\n$(selector).hide(speed,callback);<\/td>\n<td>hide and show HTML elements<br \/>\nThe optional speed parameter specifies the speed of the hiding\/showing, and can take the following values: &#8220;slow&#8221;, &#8220;fast&#8221;, or milliseconds.<br \/>\nThe optional callback parameter is a function to be executed after the hide() or show() method completes.<\/td>\n<\/tr>\n<tr>\n<td>toggle()<\/td>\n<td>$(&#8220;p&#8221;).toggle();<\/td>\n<td>toggle between hide and show, shown elements are hidden and hidden are shown.<\/td>\n<\/tr>\n<tr>\n<td>fadeIn(), fadeOut(), fadeToggle(), fadeTo()<\/td>\n<td>$(selector).fadeIn(speed,callback);<br \/>\n$(&#8220;#div1&#8221;).fadeIn();<br \/>\n$(&#8220;#div2&#8221;).fadeIn(&#8220;slow&#8221;);<\/td>\n<td>methods to fade in a hidden element, fade out a visible element, toggle between fadein and fadeout, or fadeto a given opacitiy.<\/td>\n<\/tr>\n<tr>\n<td>callback<\/td>\n<td>$(&#8220;button&#8221;).click(function(){<br \/>\n$(&#8220;p&#8221;).hide(&#8220;slow&#8221;,function(){<br \/>\nalert(&#8220;The paragraph is now hidden&#8221;);<br \/>\n});<br \/>\n});<\/td>\n<td>A callback function is executed after the current effect is 100% finished.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Manipulating Elements and Content<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Method<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>html()<\/td>\n<td>$(&#8220;p&#8221;).html(&#8220;Hello &lt;b&gt;world&lt;\/b&gt;!&#8221;);<\/td>\n<td>Can both read the current HTML inside an element and replace the current contents with some other HTML.<\/td>\n<\/tr>\n<tr>\n<td>text()<\/td>\n<td>$(&#8220;p&#8221;).text(&#8220;Hello world!&#8221;);<\/td>\n<td>like .html but does not accept html tags. Useful for replacing text within a tag.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Modify HTML and CSS<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Method<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>addClass()<\/td>\n<td>$(&#8220;p&#8221;).addClass(&#8220;intro&#8221;);<\/td>\n<td>Adds classname to the selected elements<\/td>\n<\/tr>\n<tr>\n<td>removeClass()<\/td>\n<td>$(&#8220;p&#8221;).removeClass(&#8220;intro&#8221;);<\/td>\n<td>Remove the class name from selected elements.<\/td>\n<\/tr>\n<tr>\n<td>toggleClass()<\/td>\n<td>$(&#8220;p&#8221;).toggleClass(&#8220;intro&#8221;);<\/td>\n<td>Add the class if it doesn&#8217;t exist, remove the class if it does.<\/td>\n<\/tr>\n<tr>\n<td>css()<\/td>\n<td>var bgcolor=$(&#8220;#main&#8221;).css(&#8220;background-color&#8221;)<br \/>\n$(&#8220;#body&#8221;).css(&#8220;font-size&#8221;, &#8220;200%&#8221;)<\/td>\n<td>Sets or returns one or more style properties for the selected elements..<\/td>\n<\/tr>\n<tr>\n<td>val()<\/td>\n<td>Var amount=$(&#8220;#quantity&#8221;).val();<\/td>\n<td>Set and get the value of INPUT field<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Modify HTML<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Method<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>attr()<\/td>\n<td>$(&#8220;#banner&#8221; img).attr(&#8220;src&#8221;, &#8220;images\/newImage.png&#8221;);<\/td>\n<td>Adds an attribute<\/td>\n<\/tr>\n<tr>\n<td>removeAttr()<\/td>\n<td>$(&#8220;body&#8221;).removeAttr(&#8220;bgcolor&#8221;);<\/td>\n<td>Removes an attribute.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Traversing the DOM<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Method<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>next()<\/td>\n<td>$(this).next(&#8216;.someclass&#8217;).fadeIn();<\/td>\n<td>Finds the next sibling of the current selection. Has an optional selector parameter to limit it to the next sibling of a particular type.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Miscellaneous<\/h3>\n<table>\n<tbody>\n<tr>\n<th>Method<\/th>\n<th>Usage<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>each()<\/td>\n<td>$(&#8220;button&#8221;).click(function(){<br \/>\n$(&#8220;li&#8221;).each(function(){<br \/>\nalert($(this).text())<br \/>\n});<br \/>\n});<\/td>\n<td>The each() method specifies a function to run for each matched element.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>The jQuery function $() is used to find elements on the page to work with. Common Selectors Selector Usage Description element $(&#8220;p&#8221;).hide(); The jQuery element selector selects elements based on the element name. #id $(&#8220;#test&#8221;).hide(); The #id selector &#8211; Jquery id selector uses the id attribute of an HTML tag<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_joinchat":[],"footnotes":""},"categories":[1,7],"tags":[],"class_list":["post-3384","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-web-technologies"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/posts\/3384"}],"collection":[{"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/comments?post=3384"}],"version-history":[{"count":19,"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/posts\/3384\/revisions"}],"predecessor-version":[{"id":3971,"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/posts\/3384\/revisions\/3971"}],"wp:attachment":[{"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/media?parent=3384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/categories?post=3384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmscomputer.in\/blog\/wp-json\/wp\/v2\/tags?post=3384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}