{"id":3818,"date":"2013-05-06T19:05:50","date_gmt":"2013-05-06T19:05:50","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3818"},"modified":"2013-05-06T19:05:50","modified_gmt":"2013-05-06T19:05:50","slug":"jquery-first-element-of-type","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-first-element-of-type\/","title":{"rendered":"Selecting First Element Of Type Using :first-of-type Selector in jQuery"},"content":{"rendered":"<p>The <em><strong>:first-of-type selector<\/strong><\/em> is another helpful selector in jQuery. It basically selects all elements that are the first among siblings of the same element name. That said, let&#8217;s take a look at quick examples on how to use it in real time to select any element that we need.<\/p>\n<p><!--more--><\/p>\n<h2>How To Use the\u00a0first-of-type Selector in jQuery<\/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=3818\" 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>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;style type=\"text\/css\"&gt;\n#my_div {\n\tbackground-color: #DBF0FF;\n\tborder: 1px solid #06F;\n}\n.new_div {\n\tbackground-color: #FFDF9D;\n\tborder: 1px solid #F90;\n}\n.purple {\n\tbackground-color: #9999FF;\n\tborder: 1px solid #90F;\n}\ntable td {\n\tborder: 1px solid #CCC;\n}\n\n&lt;\/style&gt;\n\n&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;span id=\"sp1\"&gt;Span 1&lt;\/span&gt;\n\n&lt;p id=\"p1\"&gt;Para 1&lt;\/p&gt;\n\n&lt;span id=\"sp2\"&gt;Span 2&lt;\/span&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\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add2\" id=\"sbt_add2\" value=\"Add Class to First p\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add2\" id=\"sbt_add3\" value=\"Add Class to First span\"&gt;&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add3\" id=\"sbt_add4\" value=\"Add Class to First td of Table\"&gt;\n\n&lt;\/p&gt;<\/pre>\n<h3>Example 1: Select first element of type div and adding 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-of-type').addClass('purple');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 2: Select first element of type p and adding 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-of-type').addClass('purple');\t\t\t\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 3: Select first element of type span and adding 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\t\n\n\t\t$('.divs_container span:first-of-type').addClass('purple');\t\t\t\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 4: Select first element of type td and adding custom class to it when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\">\t$('#sbt_add4').on(\"click\", function(e){\t\t\n\n\t\t$('.divs_container table td:first-of-type').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=3818\" 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\u00a0<em><strong>:first-of-type selector in jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The :first-of-type selector is another helpful selector in jQuery. It basically selects all elements that are the first among siblings of the same element name. That said, let&#8217;s take a look at quick examples on how to use it in real time to select any element that we need.<\/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-3818","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-ZA","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3818","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=3818"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3818\/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=3818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}