Jump to content

Getting data from form


dcole.ath.cx
 Share

Recommended Posts

Do it with DOM

<script type="text/javascript"><!--function getTxts(obj){ // many child nodesvar txt = "";var parent = document.getElementById(obj);for (var i = 0; i < parent.childNodes.length; i++) txt = txt + parent.childNodes[i].nodeValue;alert(txt);}function getTxt(obj){ // only one child nodevar parent = document.getElementById(obj);var txt = parent.firstChild.nodeValue;alert(txt);}//--></script><a id="mg" onclick="getTxt(this.id)">School</a><a id="mg2" onclick="getTxt(this.id)">School is cool</a><div id="div1">123</div><button onclick="getTxts('div1')">Get values</button>

in XHML 1.1 name attribute can be used only with form-elements.So it is good idea to use id.edit:added div and function to get many child nodes

Edited by raimo
Link to comment
Share on other sites

What's wrong with <a id='mg'>School</a>document.getElementById('mg').innerHTML??Edit: I can see Raimo's point above with multiple nodes underneath, and not wanting to use innerHTML.. but it's not bad if there's only going to be a text node in there.

Link to comment
Share on other sites

What's wrong with <a id='mg'>School</a>document.getElementById('mg').innerHTML??Edit: I can see Raimo's point above with multiple nodes underneath, and not wanting to use innerHTML.. but it's not bad if there's only going to be a text node in there.

Nothing else bad with Microsoft innerHTML, but it is not W3C DOM. :)That is not big case at all, all browsers supports it fine.But some reason I try avoid to use it whenever i can, don't know precisely why. Probably I'v seen dream where bad MS innerHTML eats all beautiful W3C DOM nodes, or something. :)Yes, it can be done with innerHTML, it's faster and easier way too.Long story, innerHTML vs DOM:http://www.developer-x.com/content/innerhtml/default.html
Link to comment
Share on other sites

[sorry for getting off the point of the thread, although I think it's been solved,really]Heh, I read the article and I must say I think even the guys in it are confused! The "for and against" at the end aren't really that conclusive, and i'd lean more towards the "for" side. It's much much faster in parsing in browsers, which I think is the main downer for promoting DOM over it. Why would you want to implement something that runs slower than what you've currently got? I think it's a bit unfair they compare it to "goto" too. I've been using VB for years now, and goto is my pet hate, and an absolute last resort in anything i've ever created!:) I like innerHTML.. what i don't like is people who try and use it as an absolute string.. like:document.body.innerHTML = document.body.innerHTML.replace(...and expect it to work effectively. I can see their reservations on teaching it to newbies, but it's such a powerful tool, I think it'd be wrong to dismiss it :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...