{"id":791,"date":"2021-10-08T12:59:00","date_gmt":"2021-10-08T16:59:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=791"},"modified":"2022-03-01T13:01:09","modified_gmt":"2022-03-01T18:01:09","slug":"jquery-quick-start","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/jquery-quick-start\/","title":{"rendered":"jQuery Quick Start"},"content":{"rendered":"\n<p>Before learning jQuery, you should learn HTML, CSS, JavaScript, and learn how to debug JavaScript. You should also be familiar with JSON. Knowing XML will also be helpful.<\/p>\n\n\n\n<p>jQuery is a free and open source JavaScript library. It simplifies common web tasks such as state initialization or updating elements and makes it easy to make AJAX calls, manipulate content, and create animations. It also does the dirty work of dealing with cross-browser compatibility issues. jQuery&#8217;s syntax is much simpler and concise than the syntax of vanilla JavaScript.<\/p>\n\n\n\n<p>JQuery uses CSS syntax for many operations. It works easily with elements and sets of elements. It can also be extended with plugins. You use free plugins or create your own.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">First example<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Add source of jQuery script<\/li><li>Write jQuery code in head<\/li><li>Add div where jQuery output will be inserted<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Selectors<\/h3>\n\n\n\n<p>Selectors and filters are used to extract information from web pages. To select an element, use<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(\"p\")\n<\/code><\/pre>\n\n\n\n<p>This selects all &lt;p&gt; html elements. The following code will select all HTML elements with the class classes with the name &#8220;myclass&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(\".myclass\")\n<\/code><\/pre>\n\n\n\n<p>The following code will select all HTML elements with the identifier &#8220;myid&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(\"#myid\")\n<\/code><\/pre>\n\n\n\n<p>Following example shows how the three types of selector are used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Selectors&lt;\/title&gt;\n    &lt;script type=\"text\/javascript\" src=\"jquery-3.1.1.js\"&gt;&lt;\/script&gt;\n    &lt;script type=\"text\/javascript\"&gt;\n        $(\"document\").ready(function() {\n            $(\"h1\").css(\"border\", \"5px solid red\");\n            $(\".myclass\").css(\"border\", \"5px solid green\");\n            $(\"#myid\").css(\"border\", \"5px solid blue\");\n        });\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Tag selector&lt;\/h1&gt;\n    &lt;h2 class=\"myclass\"&gt;Class Selector&lt;\/h2&gt;\n    &lt;h3 id=\"myid\"&gt;Id Selector&lt;\/h3&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Filters<\/h3>\n\n\n\n<p>Filters are used to narrow down results of selectors. :first selects the first instance of the selector. :last selects the last instance of the selector. p:not(p:eq(2)) means select all &lt;p&gt; tags except the one with index=2. Since indexing starts with 0, this will select all except the third. :even select even number indexed tags and :odd is for odd numbered. Since the indexing starts with 0, even would select first, third, fifth, etc. See the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Filters&lt;\/title&gt;\n    &lt;script type=\"text\/javascript\" src=\"jquery-3.1.1.js\"&gt;&lt;\/script&gt;\n    &lt;script type=\"text\/javascript\"&gt;\n        $(\"document\").ready(function() {\n            $(\"#one p:first\").css(\"border\", \"5px solid red\");\n            $(\"#one p:last\").css(\"border\", \"5px solid red\");\n            $(\"#two p:even\").css(\"border\", \"5px solid green\");\n            $(\"#two p:odd\").css(\"border\", \"5px solid blue\");\n            $(\"#three p:not(p:eq(2))\").css(\"border\", \"5px solid orange\");\n        });\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h3&gt;:first and :last&lt;\/h3&gt;\n    &lt;div id=\"one\"&gt;\n        &lt;p&gt;First paragraph&lt;\/p&gt;\n        &lt;p&gt;Second paragraph&lt;\/p&gt;\n        &lt;p&gt;Third paragraph&lt;\/p&gt;\n    &lt;\/div&gt;\n    &lt;h3&gt;:even and :odd&lt;\/h3&gt;\n    &lt;div id=\"two\"&gt;\n        &lt;p&gt;First paragraph&lt;\/p&gt;\n        &lt;p&gt;Second paragraph&lt;\/p&gt;\n        &lt;p&gt;Third paragraph&lt;\/p&gt;\n        &lt;p&gt;Fourth paragraph&lt;\/p&gt;\n    &lt;\/div&gt;\n    &lt;h3&gt;All except one&lt;\/h3&gt;\n    &lt;div id=\"three\"&gt;\n        &lt;p&gt;First paragraph&lt;\/p&gt;\n        &lt;p&gt;Second paragraph&lt;\/p&gt;\n        &lt;p&gt;Third paragraph&lt;\/p&gt;\n        &lt;p&gt;Fourth paragraph&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Editing Page Content<\/h3>\n\n\n\n<p>To edit page content, you use html(), prepend() or text() functions. html() will replace existing content. prepend() will add content in front of existing content. text() will add new content. Following code shows how they are used:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Creating and Changing Content&lt;\/title&gt;\n    &lt;script type=\"text\/javascript\" src=\"..\/jquery-3.1.1.js\"&gt;&lt;\/script&gt;\n    &lt;script type=\"text\/javascript\"&gt;\n        $(\"document\").ready(function () {\n            var newcontent = $(\"&lt;p&gt;\");\n            newcontent.append(\"Added this text\");\n            $(\"#txt1\").html(newcontent);\n            $(\"#txt2\").html(\"Replaced existing content\")\n            $(\"#txt3\").prepend(\"Added this text before \");\n            $(\"#txt4\").text(\"Adding new text\");\n        });\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id=\"txt1\"&gt;&lt;\/div&gt;\n    &lt;div id=\"txt2\"&gt;This will be replaced&lt;\/div&gt;\n    &lt;div id=\"txt3\"&gt;existing text&lt;\/div&gt;\n    &lt;div id=\"txt4\"&gt;new text&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Before learning jQuery, you should learn HTML, CSS, JavaScript, and learn how to debug JavaScript. You should also be familiar with JSON. Knowing XML will also be helpful. jQuery is a free and open source JavaScript library. It simplifies common web tasks such as state initialization or updating elements and makes it easy to make [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[54,256],"class_list":["post-791","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-quickstart"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=791"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/791\/revisions"}],"predecessor-version":[{"id":792,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/791\/revisions\/792"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}