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 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 :

 

 

 

 

 

Syntax:

 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 "roseindia.net" to the offset position "pos". Here is the full html code for the example as follows:

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

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