k-World

Selectors play a important role in jQuery to find and element in the DOM (Document Object Model). After selecting the element we can user the jQuery methods on the selected elements.

Basic Selectors in jQuery are:

1. Id Selector : In the id selector we select the element by using the id of an element. Syntax for the id selector is “#id”.

Ex: If you want to select and element which is having the id as “customerSubmit”, we can write in jQuery as

$(“#customerSubmit”).click(function(){

        alert(“Hello World”);

});

2. Css Selector : In the Css selector we select an element by using the css name of an element. Syntax for the css selector is “.”.

Ex: If you want to select an element which is having the css name as the “footer”, we can write in jQuery as

$(“.footer”).html(“Hello jQuery”);

3. Element/Type Selector : In the Type selector we select an element by using the type. Syntax for the type selector is “type”.

Ex: If you want to select an element which of type paragraph in the DOM, we can write in jQuery as

$(“p”).css(“background-color”, “red”);

References: http://api.jquery.com/

Comments on: "JQuery – Basic Selectors" (1)

  1. […] JQuery – Basic Selectors (kirangondi.wordpress.com) […]

Leave a comment