Javascript examples for jQuery Selector:only-of-type
The :only-of-type selector selects every element that is the only child of its type, of its parent.
The following code shows how to select each <p> element that is the only <p> element of its parent:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p:only-of-type").css("background-color", "yellow"); });// w ww . j a v a 2 s. c o m </script> </head> <body> <div style="border:1px solid;"> <p>The first paragraph.</p> <p>The last paragraph.</p> <div style="border:1px solid;"> <p>The first paragraph.</p> <p>The last paragraph.</p> </div><br> </div><br> <div style="border:1px solid;"> <p>The only paragraph.</p> </div><br> <div style="border:1px solid;"> <span>This is a span element.</span> <p>The only paragraph.</p> </div><br> </body> </html>