473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Accessing form elements dynamically

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
4 2013
Dasty
101 Expert 100+
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
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
101 Expert 100+
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
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

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

Similar topics

6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
1
by: adamredwards | last post by:
I have a page with some form elements that are dynamically generated. They are inserted into the dom by first cloning a node, changing the values like name, and then inserted with insertBefore(). ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.