0

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!

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by fxm
0

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.

0

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)

0

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

Thanks for help!

0

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).

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.