How to set HTML of any control using jQuery
In my previous post "How to get HTML of any control using jQuery", I had explained that how can we get the HTML of any control. In this post, I will show you how can you set HTML for any control. Well, we will use the same function html() using which we get the HTML. This function "html()" also takes an argument. Value passed in the argument will be the new content of the div. For example, I have placed a div control on the page which has h1 and h2 tag.
<div id="dvExample"> <h1> jQuery By Example Rocks.. </h1> <h2> It has really cool tips. </h2> </div>
Below jQuery code will set the div content and display the new content. I will remove h1 and h2 tag and will place h3 tag.
$(document).ready(function() { $("#dvExample").html('<h3>I am new content of the div.</h3>') alert($("#dvExample").html()); });
Output: