By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,400 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,400 IT Pros & Developers. It's quick & easy.

Accessing form elements dynamically

P: 11
Hi

I am using an external JavaScript file that should handle number of similar forms and form elements on different html pages.

What I need to do is , pass the name of the form, the two text boxes , name of a combobox and a checkbox group to the file.

As an example , inside Webpage I have:

Expand|Select|Wrap|Line Numbers
  1. < body onload="javascript:runMyCode('myform1','textbox1','textbox2','checkgroup1','combo1')">
and the external file has the following code:

Expand|Select|Wrap|Line Numbers
  1. function runMyCode(form,name,job,check,cbo) 
  2. {
  3. document.getElementById(form).getElementById(name).value='value1';
  4. document.getElementById(form).getElementById(job).value='value2';
  5. .....
  6.  
  7. }

But it is wrong and I am hoping JavaScript experts will help me solve this. Kindly tell me how to go about this.


Thank you in advance
Evrim
Nov 23 '07 #1
Share this Question
Share on Google+
4 Replies


Dasty
Expert 100+
P: 101
Function getElementById is method of document element. You can not call it on "form" DOM element. But, why dont you just call it that way?

Expand|Select|Wrap|Line Numbers
  1. function runMyCode(form,name,job,check,cbo)
  2. {
  3.   document.getElementById(name).value='value1';
  4.   document.getElementById(job).value='value2';
  5. }
Two different elements can not have same id in valid page. But don't forget to set id properties to them. like:

[HTML]<input type=text id=myid1 .......... >[/HTML]
Nov 23 '07 #2

P: 11
Function getElementById is method of document element. You can not call it on "form" DOM element. But, why dont you just call it that way?

Expand|Select|Wrap|Line Numbers
  1. function runMyCode(form,name,job,check,cbo)
  2. {
  3.   document.getElementById(name).value='value1';
  4.   document.getElementById(job).value='value2';
  5. }
Two different elements can not have same id in valid page. But don't forget to set id properties to them. like:

[HTML]<input type=text id=myid1 .......... >[/HTML]

Thank you very much for your reply ....

Could you please tell me how it is possible to access the form element itself?
For example:

Expand|Select|Wrap|Line Numbers
  1. function goPlaces(theForm,theURL){
  2.     document.theForm.action=theURL;
  3.     document.theForm.submit();
  4. }
Generates errors ...........


Thanks again
Evrim
Nov 23 '07 #3

Dasty
Expert 100+
P: 101
Expand|Select|Wrap|Line Numbers
  1. function goPlaces(theForm,theURL)
  2. {
  3.   document.theForm.action=theURL;
  4.   document.theForm.submit();
  5. }
You are sending theForm attribute as string. And you can not use string that way. But you was close. Use this instead:

Expand|Select|Wrap|Line Numbers
  1. function goPlaces(theForm,theURL)
  2. {
  3.   document[theForm].action=theURL;
  4.   document[theForm].submit();
  5. }
Dont forget that in this case "theForm" string is name (not id) of the form element.

But you can access it by id too:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('id_of_form_element').action
And you have 'forms' array in document element as well:
Expand|Select|Wrap|Line Numbers
  1. document.forms[0].action
Choose whatever you want. (but I prefer referencing any elements by id)
Nov 23 '07 #4

P: 11
You are sending theForm attribute as string. And you can not use string that way. But you was close. Use this instead:

Expand|Select|Wrap|Line Numbers
  1. function goPlaces(theForm,theURL)
  2. {
  3.   document[theForm].action=theURL;
  4.   document[theForm].submit();
  5. }
Dont forget that in this case "theForm" string is name (not id) of the form element.

But you can access it by id too:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('id_of_form_element').action
And you have 'forms' array in document element as well:
Expand|Select|Wrap|Line Numbers
  1. document.forms[0].action
Choose whatever you want. (but I prefer referencing any elements by id)

Thank you for helping me :-)
Nov 24 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.