jQuery – Get Content and Attributes

To get the content and attributes of an element using jQuery, you can use various methods and functions provided by the jQuery library.

.text(): To retrieve the text content of an element.

				
					var textContent = $('#elementId').text();

				
			

.html(): To retrieve the HTML content of an element.

				
					var htmlContent = $('#elementId').html();

				
			

.attr(): To retrieve the value of an attribute of an element.

				
					var attributeValue = $('#elementId').attr('attributeName');

				
			

.data(): To retrieve the value of a data-* attribute of an element.

				
					var dataValue = $('#elementId').data('dataAttributeName');

				
			

Here’s an example that demonstrates how to use these methods:

				
					<div id="myElement" data-custom-attr="example" class="myClass">Hello, <strong>World!</strong></div>

				
			
Join To Get Our Newsletter
Spread the love