Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JQUERY - TYPES OF SELECTORS

Note : We have explained all the selecors below. You can click here to get the details of all the selectors.

Other than class, ID and element Selectors that we have seen in the previous tutorials, JQuery also provides you with wide variety of Selectors that helps you to manage your code effectively.


Say for example, we have the below HTML code,


Example :



<html>
  	<head>
    	<title> My First Programme </title>
  	</head>

  	<body>
    	<h1> JQuery </h1>
    	<p> First Paragraph </p>
    	<p> Second Paragraph </p>
    	<p> Third Paragraph </p>
  	</body>
</html>



And in the above code, there are three <p> elements,


<p> First Paragraph </p>
<p> Second Paragraph </p>
<p> Third Paragraph </p>

Now, we want to change the content of the first <p> element.


<p> First Paragraph </p>

But the problem is, there is no class or id attributes for <p> element. So, how can we access the content of the first <p> element?


To solve this problem, JQuery provides with a selector called first followed by :. Let us modify the above code with JQuery.


Example :



<html>
    <head>
    	<title>My First Programme </title>
  	</head>

  	<body>
    	<h1> JQuery </h1>
    	<p> First Paragraph </p>
    	<p> Second Paragraph </p>
    	<p> Third Paragraph </p>
 
    	<script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script>
	  
      	<script>
      
			$('p:first').text("My New First Paragraph");
            
      	</script>    
   </body>
</html>


Output :



So, if you see the above output, we got the output changed to My New First Paragraph.


It happened due to the p:first selector,


$('p:first').text("My New First Paragraph");

That selects the first element,


<p> First Paragraph </p>

Out of the the three <p> elements,


<p> First Paragraph </p>
<p> Second Paragraph </p>
<p> Third Paragraph </p>

So, at first let us look at the widely used Selectors in JQuery :


Note : We have used some cool examples to explain each selector. Click on each selector to get the detail.





Now, let us look at the other selectors provided by JQuery.


It is not that JQuery just lets you select one class or just one element. But you can work with multiple classes or HTML elements.




When we look at the DOM. We can see that there is a close Parent - Child relationship. And JQuery helps us deal with the Parent - Child relationship effortlessly.




Just remember, HTML is a combination of elements and sometimes there are no parent child relationship. Even in such cases JQuery provides some cool selectors to deal with them.




An HTML element can have an attribute, like a class or an id. And JQuery provides excellent selectors to deal with HTML attributes.




Last but not the least, comes the forms. And JQuery selectors comes handy to deal with HTML forms.