Total Page Preview:   000000000115

What is jQuery attr()?

jQuery attr()

In jQuery, the attr() method is used to get or set attributes and their values for HTML elements. It can be used to retrieve the value of an attribute or set the value of an attribute for a set of matched elements. Let's explore the attr() method with an example:

Example:
Consider the following HTML:
In this example:
  1. Initially, there is an img element with the ID "myImage" and the src attribute set to "original.jpg."
  2. The jQuery code inside the <script> element uses the attr() method to:
  • Retrieve the original value of the 'src' attribute using $("#myImage").attr("src").
  • Log the original value to the console.
  • Set a new value for the 'src' attribute using $("#myImage").attr("src", "new-image.jpg").
  • Retrieve the updated value of the 'src' attribute.
  • Log the updated value to the console.
This example demonstrates how to use the attr() method to get and set attribute values. In this case, it retrieves the original 'src' value, updates it to a new value, and then retrieves and logs the updated value.
 
 
 
 
 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments