9 Replies - 1353 Views - Last Post: 19 March 2012 - 12:16 AM

#1 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

How to get all form reference in a document

Posted 18 March 2012 - 10:38 PM

I'm trying to get all forms in a doc. But following code gives just first forms, and then says document.forms[i] is undefined.
function getForms() {
//document.write("in fun");
     totalForms = document.getElementsByTagName('form');
     formsLength = document.forms.length;
for(i=0; i<formsLength; i++) {
    document.write(document.forms[i].name);
}
      document.write("formslength : " + formsLength);
 }


This post has been edited by Dormilich: 18 March 2012 - 11:26 PM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How to get all form reference in a document

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:27 PM

when do you call that code?
Was This Post Helpful? 0
  • +
  • -

#3 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:34 PM

View PostDormilich, on 18 March 2012 - 11:27 PM, said:

when do you call that code?


At the end of the HTML file with forms.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:35 PM

before or after the page finished loading?
Was This Post Helpful? 1
  • +
  • -

#5 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:39 PM

View PostDormilich, on 18 March 2012 - 11:35 PM, said:

before or after the page finished loading?

I hope it's after page finished loading. Let me paste both html and js here. This is a simple html doc.
<html>
<body>
<form name="txtForm">
<div><input type="text" id="ftxt" name="ftxt" value="Are you happy?"></div>
</form>
<a href="#" id="yes">YEs</a>
<a href="#" id="no">No</a>

<b>2.   Text Area value Manipulation:</b>
<br>
<form name="txtForm2">
	<div><textarea name="multiLineText" id="multiLineText" rows="10" cols="25" disabled></div>
	Mouse over below to read question.
	</textarea>
	</form>
	<a href="#" id="question">Question</a>
	<a href="#" id="answer">Answer</a>
<script src="js/tforms.js"></script>
</body>
</html>	

function getForms() {
//document.write("in fun");
     totalForms = document.getElementsByTagName('form');
     formsLength = document.forms.length;
	 for(i=0; i<formsLength; i++) {
	     document.write(document.forms[i].name);
		 
		 }
		 
      document.write("formslength : " + formsLength);
	  }
	  
window.onload = function() {
        getForms();
		}

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:42 PM

View Postsonalmac, on 19 March 2012 - 07:39 AM, said:

I hope it's after page finished loading. Let me paste both html and js here. This is a simple html doc.

and that's why your code fails. document.write() kills your page when called after the page finished loading. that's why no-one normally uses this function.
Was This Post Helpful? 1
  • +
  • -

#7 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:48 PM

View PostDormilich, on 18 March 2012 - 11:42 PM, said:

View Postsonalmac, on 19 March 2012 - 07:39 AM, said:

I hope it's after page finished loading. Let me paste both html and js here. This is a simple html doc.

and that's why your code fails. document.write() kills your page when called after the page finished loading. that's why no-one normally uses this function.


oh wow, that's all... I just changed it to console.log and now my script works. I thought I can use it just for my practice, but it will make it this difficult... thanx a lot.....
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:50 PM

sadly enough there are too many outdated beginners' tutorials that start you with bad practices like that.
Was This Post Helpful? 0
  • +
  • -

#9 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: How to get all form reference in a document

Posted 18 March 2012 - 11:56 PM

View PostDormilich, on 18 March 2012 - 11:50 PM, said:

sadly enough there are too many outdated beginners' tutorials that start you with bad practices like that.

I realize that. But I started reading Larry Ullman's Moder Javascript: develop and design. It teaches how to separate js and html. Although my college course for js is still using those inline js codes, i'm trying to get to new approach. And ofcourse, Larry is not using document.write, anywhere.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: How to get all form reference in a document

Posted 19 March 2012 - 12:16 AM

View Postsonalmac, on 19 March 2012 - 07:56 AM, said:

Although my college course for js is still using those inline js codes, i'm trying to get to new approach.

you should look for 2 things as next big steps:
- Event Listeners (replacing the inline event attributes. unfortunately IE does it differently up to IE8. and I have to admit that the event flow (though crucial, since JS is an event driven language) is not easy to understand)
- Objects (Javascript almost completely consists of objects*. once you get that JS becomes way more understandable. and you can write better code)


* - non-objects in JS: null, undefined (with some quirks) and the boolean/string/number literals (though they can be converted to objects on-the-fly)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1