Friday, February 1, 2013

How to dynamically insert html code/content into a web page using jQuery?

To dynamically insert html code/content into a web page, we can use ".html(htmlstring)" method of jQuery. Where "htmlstring" is a HTML string which is to be set as content of each and every matched elements.

When we use the method ".html(htmlstring)" to set any element's content, it will not only replace complete content in that element with the new content but also removes other constructs like data and event handlers from child elements before replacing new content.

Internally jQuery uses the browsers "innerHTML" property for this functionality.

Note: We cannot use this method for XML documents.

Inject html code/content using ".html(htmlstring)" method:

$(document).ready(function(){
$("div.button").click(function () {
$(this).html("<input type='button'; value='Button'>");
});
$("div.checkbox").click(function () {
$(this).html("<input type='checkbox' name='check' value='check'>Check Box</input>");
});
$("div.radiobutton").click(function () {
$(this).html("<input type='radio' name='radio' value='radio'>Radio Button</input>");
});
$("div.textbox").click(function () {
$(this).html("<input type='text' name='text' value='text'>Text box</input>");
});
});

Example:

<!DOCTYPE html>
<html>
<head>
<style>     
  div {padding:0px;margin:0px;cursor:pointer;}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div.button").click(function () {
$(this).html("<input type='button'; value='Button'>");
});
$("div.checkbox").click(function () {
$(this).html("<input type='checkbox' name='check' value='check'>Check Box</input>");
});
$("div.radiobutton").click(function () {
$(this).html("<input type='radio' name='radio' value='radio'>Radio Button</input>");
});
$("div.textbox").click(function () {
$(this).html("<input type='text' name='text' value='text'>Text box</input>");
});
});
</script>
</head>
<body>
<b>Click on the below items to insert html elements:</b><br/>
<div class="button">Click here to insert a Button.</div>
<div class="checkbox">Click here to insert a Checkbox.</div>
<div class="radiobutton">Click here to insert a Radio button.</div>
<div class="textbox">Click here to insert a Textbox.</div>
</body>
</html>

Sample Output:


How to dynamically insert html code/content into a web page using jQuery

Demo:



2 comments:

  1. How is the user supposed to be able to edit these generated fields?

    ReplyDelete
  2. An expert from tech looks after has got performed a necessary factor in the release belonging to the wrist chronograph plus its a great innovator during this complications. This firm possesses shown many of the greatest experiences while in the conquest on the heavens to its secure, reliable and-performance instruments. The modern world simply big check out brand name towards render a varieties together with chronometer-licensed measures, the right expression involving exquisitely detailed. However, not merely that look at, moreover introduced several priceless walls clocks. You will find quite a few feedback for your wrist watches. The excess text messaging in the 24 hours indices seldom intrude around the pre-occupied dial; one other green arrow offer gives instant indication from the hometime straight away replica omega. This business consequently does not influence on the reliability with the observe http://www.attrinity.com, none around the effectiveness in the chronograph. The newest see introduced just for this style hails from some people, solely constructed and also produced inside the company courses. The software elements similar initial structures together with your column take, some sort of top to bottom coupling motorola clutch insuring high-precision initial, plus progressive branded recentering instrument with the zero-resetting hammers. Anytime made use of in conjunction with the chronograph, the software will allow anyone that will evaluate imply pace long lasting time period passed, the length taken care of and / or this acceleration gotten to. The sit back and watch is amazingly reliable and the sacrifice regions are plentiful within virtually any sepcialist. Just like each alternate wrist watches, you could attain repairs and maintenance not to mention daily system from this chronograph globally. This is the enthusiast product together with suggestions about her surgery combined with thoughtful overhaul is offered around the globe. Any time you want to, typically the eatches might go lower equally well, this approach bargain Party Gift is a superb idea capability to deliver that will takes pleasure in looks after replica watches uk. Whilst all of these wristwatches can be pure reproductions of vintage designs on the initial trademark models, they are just so exactly the same to your prospects this possibly even experts are occasionally can not tell them away from each other. This Different watches will be about superior quality and tend to be produced making use of robust chrome steel. These consists of a gorgeous african american tone that comes with a wrist watch bracelets in which consists of crocodile set.

    ReplyDelete

Subscribe via Email