Site icon Dotnet Helpers

JQuery Selector

JQUERY SELECTORS :

1) selecting a particular element having a specific class

$(“.class1”).css(“border”, “1px solid red”);

2) select all elements of the page

$(“*”).css(“border”, “1px solid red”);

3) select an element that having a particular id

$(“#p1”).css(“border”, “1px solid red”);

4) select all specific elements type

$(“p”).css(“border”, “1px solid red”);

5) select multiple elements in a single selector

$(“div.class1, #div1”).css(“border”, “1px solid red”);

Note : From above the selector first select div having the class name with
            “.class1” and select element having id as “div1”  

 6) select a specific child of the parent element

$(“#div2 p:nth-child(2)”).css(“border”, “1px solid red”);

Note : select the 2nd paragraph (p) element that is inside the div
             element whose id is “div2” and apply the css on the element

7) select last child of the parent element

$(“#div2 p:last-child”).css(“border”, “1px solid red”);

8) select the first child of the parent element

$(“#div2 p:first-child”).css(“background”, “red”);

Note : select the first paragraph (p) element which is inside the
            div element whose id is “div2” and  apply the css on the element.

9) select an element based on its attribute

$(‘input[id$=”txtAddress”]’).val(‘Testing’);

Note : select the textboxes having id ending with “txtAddress” and set its value as “Testing”.

Exit mobile version