{"id":3812,"date":"2013-05-05T19:05:51","date_gmt":"2013-05-05T19:05:51","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3812"},"modified":"2013-05-05T19:05:51","modified_gmt":"2013-05-05T19:05:51","slug":"jquery-selector-first-child","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-selector-first-child\/","title":{"rendered":"Selecting First Level Child Using :first-child Selector In jQuery"},"content":{"rendered":"<p>If you have many elements in a web page but need to select only the first one among them and perform your operation on it, what would you do? If you don&#8217;t have an answer, then this article is for you. In this article, I am going to share very easy way to perform operations on the <em><strong>first level child<\/strong><\/em> of any desired element. \u00a0So read on to find out more.<\/p>\n<p><!--more--><\/p>\n<h2>How To Use\u00a0:first-child Selector<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3812\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p><strong><em>The first-child selector basically selects all elements that are the first child of their parent.<\/em><\/strong><\/p>\n<p>Let&#8217;s take a look at a quick example. The following is the HTML Source code used in the examples.<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;div class=\"divs_container\"&gt;\n\n&lt;div id=\"my_div1\"&gt;This is My Div 1.&lt;\/div&gt;\n\n&lt;div id=\"my_div2\"&gt;This is My Div 2.&lt;\/div&gt;\n\n&lt;div id=\"my_div3\"&gt;This is My Div 3.&lt;\/div&gt;\n\n&lt;div id=\"my_div4\" class=\"new_div\"&gt;This is My Div 4\n\n&lt;p id=\"p1\"&gt;Para 1&lt;\/p&gt;\n\n&lt;p id=\"p2\"&gt;Para 2&lt;\/p&gt;\n\n&lt;p id=\"p3\"&gt;Para 3&lt;\/p&gt;\n\n&lt;\/div&gt;\n\n&lt;br&gt;\n\n&lt;table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\"&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n    &lt;td&gt;&amp;nbsp;&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/table&gt;\n\n&lt;\/div&gt;\n\n&lt;p&gt;\t\n  &lt;input type=\"submit\" name=\"sbt_add1\" id=\"sbt_add1\" value=\"Add Class to First Div Child\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add2\" id=\"sbt_add2\" value=\"Add Class to First p child\"&gt;&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add3\" id=\"sbt_add3\" value=\"Add Class to First child of Table\"&gt;\n\n&lt;br&gt;\n&lt;input type=\"button\" name=\"sbt_reset\" id=\"sbt_reset\" value=\"Reset everything\"&gt;\n\n&lt;\/p&gt;<\/pre>\n<h3>Example 1: Select first level child div and add custom class to it when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\">\t$('#sbt_add1').on(\"click\", function(e){\t\t\n\n\t\t$('.divs_container div:first-child').addClass('purple');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 2: Select first level child p tag and add custom class to it when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\">\t$('#sbt_add2').on(\"click\", function(e){\t\t\t\n\n\t\t$('.divs_container p:first-child').addClass('purple');\t\t\t\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 3: Select first level child table cell and add custom class to it when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\">\t$('#sbt_add3').on(\"click\", function(e){\t\t\n\n\t\t$('.divs_container table td:first-child').addClass('purple');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3812\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>Simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to use the <em><strong>:first-child selector in jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have many elements in a web page but need to select only the first one among them and perform your operation on it, what would you do? If you don&#8217;t have an answer, then this article is for you. In this article, I am going to share very easy way to perform operations [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[42,43,41],"class_list":["post-3812","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-demos-2","tag-jquery-snippets","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-Zu","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3812"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3812\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}