Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

accessing the value in a DOM tree  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have html document. In that document I have some code that looks like the following:
<tr>
<td>Name:</td>
<td>
<div class="Data" id="login_name">
<input type=text></input>
</div>
</td>
</tr>
The user enters in their login name (and their password in another input box not shown here) and presses a button on the page that says "login". The button has onclick="login(); function. In "login()" to access what the user entered in the input box by accessing the DOM tree. Here is what I am doing right now.
// the login is actually a var that can contain the login
// name or password. This returns an object
var element = document.getElementById("login_name");
// child = INPUT, which it should
var child = element.firstChild.nodeName;
[don't know what the next line here should be]
What I am trying to do is access is the value a user types into the input box. I can look at the DOM tree and see:
+ DIV = Name IV Type:ELEMENT_NODE Att:class=Data,id=login_name
INPUT - Name:INPUT Type:ELEMENT_NODE Att: value=bob
note that the user here types bob in thee login text area. I've tried various ways to access this "bob" value but cannot find the correct method. I will need to be able to read/write this text area. Should I change my actual HTML and put XML tags around the <input type=text></input> to something like <LOGINNAME><input type=text></input></LOGINNAME> and this will create a way to access the value in the input box? Any help would be appreciated.
Thanks in advance,
Joe Crew
[ Ajith disabled smilies in the post so that the colon prefixed characters are not interpreted as UBB images ]
[This message has been edited by Ajith Kallambella (edited April 26, 2001).]
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe, it�s not completely clear where XML fits into picture. You said that you have HTML document. If so, you do not need to worry about XML at all. In this case DOM (Document Object Model) is an object model of your HTML page. If you want to read
value of text field, you can read its �value� property using JavaScript. But in this case your id should be on <input> element itself, not on <div>:
<div class="Data">
<input type=text id="login_name"></input>
Then in your login() function you can write:
login ()
{
var element = document.getElementById("login_name");
alert (element.value );
}
Does it help?
 
Joe Crew
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mapraputa,
You were 100% correct in your response. Thank you so very much. I am kind of new to this so I did kind of asked the question incorrectly. But you saw through it and gave me the correct answer anyway. You were right in that it was not XML but just a DOM tree itself, and I was just trying to access the value in an input box.
Thanks again,
Joe Crew
 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe, thank you for asking!
When we approach a new field it is difficult even to formulate the problem, so do not worry about �incorrect� questions. We all learn here and �incorrectly asked� question are the most useful (at least, for those who answer ). I remember somebody here said that �ill-defined� questions are more useful than �well-defined�, because they make us actually think, instead of just rearranging words. That what I felt answering college tests - often it is possible to answer by repeating words, without understanding what I am saying - qustions are too correct!
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!