jQuery  Set Content and Attributes

To set the content and attributes of an HTML element using jQuery, you can use the following methods:

  1. .html() method: This method sets the content of an HTML element. For example, to set the content of a div element with an ID of “myDiv”, you can use the following code:
$('#myDiv').html('This is my new content!');

2.text() method: This method sets the text content of an HTML element. For example, to set the text content of a div element with an ID of “myDiv”, you can use the following code:

$('#myDiv').text('This is my new text content!');

3.attr() method: This method sets the value of an attribute of an HTML element. For example, to set the “src” attribute of an image element with an ID of “myImage”, you can use the following code:

$('#myImage').attr('src', 'new-image.jpg');

4.prop() method: This method sets the value of a property of an HTML element. For example, to set the “disabled” property of a button element with an ID of “myButton”, you can use the following code:

$('#myButton').prop('disabled', true);

These methods can be chained together to set multiple content and attribute values at once. For example:

$('#myDiv')
  .html('This is my new content!')
  .attr('class', 'new-class')
  .prop('disabled', true);

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *