Hi!

I want to insert an elelent (textbox) under another textbox that already exists, and then retriever the value from it for later use

i tried with this, but no success:

function newtxtbox(){
    var box = document.createElement("<INPUT TYPE='TEXT' NAME='RADIOTEST' VALUE='First Choice'>")
    document.body.insertBefore(box, existingbox);

}

Thanks in advance!

insert (textbox)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <title></title>
  </head>
  <body>
    <form>
      <input id="IT">
    </form>
    <script type="text/javascript">
	var oITnew = document.createElement("input");
	oITnew.setAttribute("value","added");
	var oIT = document.getElementById("IT");
	var oPAR = oIT.parentNode;
	oPAR.insertBefore(oITnew,oIT);
    </script>
  </body>
</html>

An easy way to access the new element from javascript is to set its id.

well, this doesnt seem to work for me, as i apparently cant insert it as the element before doesnt have an id (i can return the element by name)

this is my (a bit) modified code:

function newbx(){
var otex = new Array();
var otex = document.getElementsByName("existingelement");
var oITnew = document.createElement("input");
oITnew.setAttribute("value","added");
var oPAR = oIT.parentNode;
oPAR.insertBefore(oITnew, otex[0]);
}

i cant seem to find where i am wrong :S

(thanks for your reply anyways)

Got it to work, i found the id so i can put it before the element
so, it works now

Thanks for help!

i found the id so i can put it before the element

To put it after, use oPAR.insertBefore(oITnew,""); (assuming that oPAR is defined as before).

Member Avatar for rajarajan2017

Thanks Its helped me too..

To put it after, use oPAR.insertBefore(oITnew,""); (assuming that oPAR is defined as before).

Two problems:
1. "after" is a careless choice of word. The element will be inserted at the end of the sibling list (which in this example means that it will be after the existing element).
2. "" as a second argument is not supported in all browsers; use oPAR.insertBefore(oITnew,null); instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.