Hot Posts

6/recent/ticker-posts

JQuery Elements | Selecting Elements

Selecting Elements

jQuery is a fast, small, and feature-rich JavaScript library that makes HTML document traversal and manipulation, event handling, and animation much simpler with an easy-to-use API that works across a multitude of browsers. Here’s an overview of some of the most commonly used jQuery elements and functions:

1. Selecting Elements

Basic Selectors
  • $("element"): Selects all elements with the specified tag name.
     $("p") // selects all <p> elements
  • $(".class"): Selects all elements with the specified class.
    $(".myClass") // selects all elements with class "myClass" 
  • $("#id"): Selects the element with the specified ID.
     $("#myId") // selects the element with id "myId"
  • Attribute Selectors
  • $("[attribute='value']"): Selects elements with the specified attribute and value.
  • $("[type='text']") // selects all elements with type="text"
    Combination Selectors
  • $("element.class"): Combines tag and class selectors.
  • $("div.myClass") // selects all <div> elements with class "myClass"

2. Manipulating Elements

CSS Manipulation

$(element).css("property", "value"): Sets a CSS property for the selected elements.

$("p").css("color", "red") // changes the text color of all <p> elements to red 
Adding/Removing Classes
  • $(element).addClass("className"): Adds a class to the selected elements.
  • $("p").addClass("highlight") // adds the "highlight" class to all <p> elements
  • $(element).removeClass("className"): Removes a class from the selected elements.
  • $("p").removeClass("highlight") // removes the "highlight" class from all <p> elements
    HTML and Text
  • $(element).html("new HTML"): Sets the HTML content of the selected elements.
  • $("#myDiv").html("<p>New content</p>") // changes the HTML content of the element with id "myDiv" 
  • $(element).text("new text"): Sets the text content of the selected elements.
  • $("#myDiv").text("New text content") // changes the text content of the element with id "myDiv"

    3. Event Handling

    Basic Event Handlers
  • $(element).click(function): Attaches a click event handler to the selected elements.
  • $("#myButton").click(function() {
             alert("Button clicked!");
             });
  • $(element).on(event, childSelector, data, function): Attaches an event handler for one or more events to the selected elements.
  • $("p").on("click", function() {
           $(this).hide();
           });

    4. Effects and Animations

    Showing and Hiding
  • $(element).hide(): Hides the selected elements.
  • $("p").hide() // hides all <p> elements
  • $(element).show(): Shows the hidden elements.
  • $("p").show() // shows all <p> elements
    Fading
  • $(element).fadeIn(): Fades in the selected elements.
  • $("#myDiv").fadeIn() // fades in the element with id "myDiv"
  • $(element).fadeOut(): Fades out the selected elements.
  • $("#myDiv").fadeOut() // fades out the element with id "myDiv"

    5. AJAX

    Loading Data

    $.ajax(): Performs an asynchronous HTTP request.

         $.ajax({
         url: "https://api.example.com/data",
         method: "GET",
         success: function(response) {
        console.log(response);
        },
        error: function(error) {
        console.error(error);
      }
      });
  • $.get(url, success): Loads data from the server using a HTTP GET request.
  • $.get("https://api.example.com/data", function(response) {
      console.log(response);
    });
  • $.post(url, data, success): Loads data from the server using a HTTP POST request.
  • $.post("https://api.example.com/data", { key: "value" }, function(response) {
      console.log(response);
    });
    

Jquary Elements

Conclusion

jQuery provides a powerful set of tools to simplify JavaScript programming, making it easier to work with HTML documents, handle events, create animations, and perform AJAX operations. This overview covers some of the most commonly used jQuery elements and functions to help you get started.

Post a Comment

0 Comments