Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I get javascript variables into DOM->XML?

Status
Not open for further replies.

tranquillo

Technical User
Jan 6, 2003
22
SE
Hi...

I'm trying to make a form for adding elements into a xml file using javascript and DOM. I've tested the script on w3schools.com and most of the code works just the way I want it to. the problem is getting the value of a textfield from the form into the DOM part.
is there some DOM syntax that I have to use to fetch the values from the form?

here's the code:
the parts where the value is inserted direktly works fine. it's the one that gets the value from the form that's the problem.
(form name:form, textfield name:isbn, form uses "onsubmit" to call the funktion "skapa()")
######################################
<script type=&quot;text/javascript&quot;>
function skapa()
{
var xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;)
xmlDoc.async=&quot;false&quot;
xmlDoc.load(&quot;test.xml&quot;)

document.write(&quot;The original XML:&quot;)
document.write(xmlDoc.documentElement.text)

var newnode=xmlDoc.createElement(&quot;produkt&quot;)
document.write(&quot;*&quot;)

var isbn = document.form.isbn.value
document.write(&quot;*&quot;)

var child1=xmlDoc.createElement(isbn)
var child2=xmlDoc.createElement(&quot;forfattare&quot;)
var child3=xmlDoc.createElement(&quot;titel&quot;)
var child4=xmlDoc.createElement(&quot;antal&quot;)
var child5=xmlDoc.createElement(&quot;kostnad&quot;)
var child6=xmlDoc.createElement(&quot;rabatt&quot;)

var childcontent1=xmlDoc.createTextNode(&quot;123&quot;)
var childcontent2=xmlDoc.createTextNode(&quot;olof palme&quot;)
var childcontent3=xmlDoc.createTextNode(&quot;mitt liv som hund&quot;)
var childcontent4=xmlDoc.createTextNode(&quot;1&quot;)
var childcontent5=xmlDoc.createTextNode(&quot;60&quot;)
var childcontent6=xmlDoc.createTextNode(&quot;0&quot;)
document.write(&quot;*&quot;)

child1.appendChild(childcontent1)
xmlDoc.documentElement.appendChild(child1)
child2.appendChild(childcontent2)
xmlDoc.documentElement.appendChild(child2)
child3.appendChild(childcontent3)
xmlDoc.documentElement.appendChild(child3)
child4.appendChild(childcontent4)
xmlDoc.documentElement.appendChild(child4)
child5.appendChild(childcontent5)
xmlDoc.documentElement.appendChild(child5)
child6.appendChild(childcontent6)
xmlDoc.documentElement.appendChild(child6)
document.write(&quot;*&quot;)

newnode.appendChild(child1)
newnode.appendChild(child2)
newnode.appendChild(child3)
newnode.appendChild(child4)
newnode.appendChild(child5)
newnode.appendChild(child6)
xmlDoc.documentElement.appendChild(newnode)
document.write(&quot;*&quot;)


document.write(&quot;After appending the new node:&quot;)
document.write(xmlDoc.documentElement.text)
}
</script>
######################################
 
If your form were like this :

Code:
<form>
  <input type=&quot;text&quot; name=&quot;isbn&quot; value=&quot;abc123&quot;>
</form>

then the line to get that value would be :

document.form[0].isbn.value


Or, if you give your form a name :
Code:
<form name=&quot;myform&quot;>
  <input type=&quot;text&quot; name=&quot;isbn&quot; value=&quot;abc123&quot;>
</form>

then the line to get that value would be :

document.myform.isbn.value
 
thanks, but I allready knew that.

so why isn't it working?

the only thing that doesn't work is where I try to fetch the value from the form.
 
Yes but your code example says :

Code:
   var isbn = document.form.isbn.value

NOT as it should be :

Code:
   var isbn = document.forms[0].isbn.value
 
tried that to, and everything else I can think of... nothing seems to work...

is there something about DOM that just hates the document.form.x.value function?
I've tried all kinds off diferent things but each time I add the .value it stops reading at that point...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top