473,503 Members | 7,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

var for form element name

Hello,

I'm trying to pass a var to a function for it to be used as the name of
a form element.

IE:

function doThis(form, elemName) {

if( form.elemName.value == false ) {
// do something
}

}

I can't seem to figure out how to use the var where the name goes.

Thanks,

- D

Nov 2 '05 #1
6 1666
i was able to use
function doThis(formName, elemName) {

if( document.forms[formName].elemName.value == false ) {
// do something
}

}

Nov 2 '05 #2
dstefani wrote:
I'm trying to pass a var to a function for it to be used as the name of
a form element.

IE:
The user agent does not matter, this feature is supported by all JS-capable
HTML UAs (part of the so-called DOM Level 0).
function doThis(form, elemName) {

if( form.elemName.value == false ) {
Use bracket property accessor syntax to access an element of the collection:

if (form.elements[elemName].value)
{

Since the `value' property of objects referring to form controls is of type
string, it can never be `false'. You could test for the empty string (x ==
'' or x.length == 0), or you could just use automatic type conversion as I
did.
// do something
}

}

I can't seem to figure out how to use the var where the name goes.


<http://jibbering.com/faq/#FAQ4_13>
HTH

PointedEars
Nov 2 '05 #3
Excellent, thanks.

I haven't had to do any JS stuff in a while. Serious cobwebs!

Thanks,

- D

Nov 2 '05 #4
Thomas 'PointedEars' Lahn wrote:
dstefani wrote:
function doThis(form, elemName) {

if( form.elemName.value == false ) {


Use bracket property accessor syntax to access an element of the
collection:

if (form.elements[elemName].value)
{


Additional note: Since I do not know which value you are passing for `form',
I have to consider that it may be a number or string identifying the form
element by its index, name or ID, instead of a reference to a
HTMLFormElement object. In this case, you would need

if (document.forms[form].elements[elemName].value ...)
{
// ...
}
PointedEars

P.S.: Please quote what you are replying to, see
<http://jibbering.com/faq/>.
Nov 2 '05 #5
Well the above works, but now I'm trying to go back and focus on the
errant field, like so:

<code>

if ( TE_digit(form.elements[elemName].value) == false) {
alert("Yatta, yatta, yatta");
var elem = form.elements[elemName];
elem.focus();
return false;
}
</code>

It still won't focus back on the field that it tested.

What am I missing?

Thanks

- D

Nov 2 '05 #6
dstefani wrote:
Well the above works,
Which "above" are you talking about?
<http://www.jibbering.com/faq/faq_notes/pots1.html#ps1Post>
but now I'm trying to go back and focus on the
errant field, like so:

<code>

if ( TE_digit(form.elements[elemName].value) == false) {
alert("Yatta, yatta, yatta");
var elem = form.elements[elemName];
elem.focus();
return false;
}
</code>

It still won't focus back on the field that it tested.


Probably there is more than one form control with that name in
which case all so-named form controls are members of a collection
returned by form.elements[elemName]. The JavaScript console should
show an script error or Exception on the elem.focus() line then.

Otherwise you will have to show more code, best by posting a public
URI for your HTML document.
PointedEars
Nov 2 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3070
by: Citoyen du Monde | last post by:
Trying to get some ideas on a simple javascript project (to teach myself the language). I want to develop a client-side vocabulary practice application that would allow users to enter their own...
12
2437
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
2
2892
by: Asad | last post by:
I have a form on a page that has several textareas, and textboxes inside a table (so the table containing the textboxes is also inside the FORM tag). I want to replace the textareas with simple...
4
2710
by: multimatum2 | last post by:
Hello, I need to enable/disable input text forms... But... I need to have the same style (color...) in both modes.. Could you help me ? Thanx a lot A small sample... ...
3
2839
by: Skippytpe | last post by:
Does anyone have an idea why the form validation in the following page wouldn't be working? I had been using XHTML 1.0 transitional which allowed me to use the form attribute 'name.' I could then...
4
9281
by: Stuart Perryman | last post by:
Hi, I have the following code which works just fine in IE6 but not in Firefox. It is an extract of several table rows each with an individual form. It is generated by php. <form...
6
2715
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...
5
1875
by: ojvm | last post by:
ok. thanks again for the time spend reading this. this code adds 2 controls in html form but it places in top of the form. i want this control1 control2 control1 control2 control1 ...
3
3423
by: michael | last post by:
let me keep it clean, quick and simple. I am passing a variable into another window and am reassigning the value on the new page - window.document...value = opener.document. ....value and...
9
1544
by: whisher | last post by:
Hi. I've managed this simple snippet: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Register</title> <meta...
0
7067
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...
1
6975
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
7449
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
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
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...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.