What are the different type of selectors in Jquery

In Jquery we can select or get an element in different ways. Here the type of selectors with examples...

We can get elements using these below classifications
1.Element Id
2.Element by Class
3.By Attribute
4.By Hierarchy CSS

Element Id


By "Id" of the element we can get the value of it .see the example


<div id="hello">Hello Dotnetspiders</div>

<script type="text/javascript">

//Select the element by it's ID

alert($("#hello").text()); // Hello Dotnetspiders

</script>



Element by Class


By "Class" of the element we can get the value of it. See the example


<div calss="hello">Hello Dotnetspiders</div>
<script type="text/javascript">

//Select the element by it's class

alert($(".hello").text()); // Hello Dotnetspiders

</script>


By Attribute



<input type="text" name="name" value="Dotnetspiders" />

<input type="text" name="Age" value="10" />

<script type="text/javascript">

//Select the element by it's attribute

alert($("input[name=name]").val()); //Dotnetspider

alert($("input[name=Age]").val()); //10

</script>



By Hierarchy CSS


Simpler to how we apply css to elements in the same way we can use the hierarchy to get the elements

In the following code we are going to apply background color to tr..


<table>

<tr><td>Dotnetspider</td></tr>

<tr><td>Dotnetspider</td></tr>

<tr><td>Dotnetspider</td></tr>

</table>
<script type="text/javascript">

$("table tr").css("background-color","yellow");

</script>


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: