Share on Google+Share on Google+

JavaScript insertData method

JavaScript method insertData() can be used to insert some specific string value to the position which is specified by the user.

JavaScript insertData method


JavaScript method insertData() can be used to insert some specific string value to the position which is specified by the user. Syntax for using the insertData() method is as follows :







 elementObject.insertData(offsetPosition, string);

where offsetPosition is that specified position where we want to insert data and string carries the data which is to be inserted.

Description of code:

In the following code we are going to illustrate how to use insertData() method for inserting the string text to the specified position in the object. We have created an html page which contains one text input and one button when user clicks on this button it calls the method funcInsertData() in which we gets the position offset value and stores it to the object pos

var pos = document.getElementById("t1").value;

Further we gets the parent element object to which we have to insert data for example in our code its "para" i.e paragraph.

var m = document.getElementById("para").firstChild;

Now we inserts data "" to the offset position "pos". Here is the full html code for the example as follows:

<script language="JavaScript">
function funcInsertData() {
var pos = document.getElementById("t1").value;
var m = document.getElementById("para").firstChild;
m.insertData(pos, '')}
<div align="center">
<table border="0" width="42%" height="55" bgcolor="#800080">
<td width="100%" height="51">
<h1 align="center"><font color="#FFFFFF">insertData method</font></h1>
<p align="center"> The Data would be inserted below</p> 
<p id="para" align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<p align="center">
<input type="text" value="" id="t1"/>
<input type="button" value="InsertData Position" onclick="funcInsertData();">

Output :

Please provide the data position to insert data.

After inserting the position click on InsertData Position button. It will insert data 

You can also download the full source code for the insertData() method use from the following link.

Download Source Code




Posted on: January 29, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JavaScript insertData method  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image