473,406 Members | 2,404 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 text form fields name=field[label]

I'm pretty much a JavaScript novice. I'm good at learning by example
and changing those examples to suit my needs. That said ....

..... I have some select fields in a form I created for a database search
that I am unable to figure out how to access. (The search is
implemented in Php/MySQL.)

The user enters search values for: name, address1, city, .... etc.,
..... and for each of these they also select whether the search should
look for the field to be equal to, not equal to, Like, or Not Like the
value in the database.

So in the form I have:
<input type=text name=city>
in combination with
<select name=func[city]>
<option value='='>=</option>
<option value='!='>!=</option>
<option value='LIKE'>LIKE</option>
<option value='!='NOT LIKE>NOT LIKE</option>
</select>

For an example, this search could be to find an entry "WHERE city
$func[city]] $city. (Php - e.g., WHERE city = 'Denver'). This works
fine - in Php. I use similar combinations for name, address, etc., ...

Okay, here's the JavaScript problem. For the Reset action I have a
JavaScript function to Reset all the form fields. I want to Reset the
selected indexes for the "func" fields (func[name], func[city],
func[state], ...) using a loop that goes through the associative array,
"func." I cannot find the right syntax to address them as an array and
I am beginning to think that JavaScript does not see them as an array.
I could have just as well named them func_name, func_address, func_city,
..... (??)

I have already come up with a workaround. I loop though *every* form
element and if substring(0,5) = 'func[' I set the SelectedIndex for that
element to 0.

What I would like is a definitive answer. Am I correct that JavaScript
does not see func as an array, or is there a syntax for accessing it
that has so far totally eluded me.

I can access individual members of func like so:
alert (form["func[host_id]"].selectedIndex);
and that works, but I cannot figure out the correct syntax to loop
through the array with:

for (x in form.func)

Is it simply not possible? If it is possible, what is the correct
syntax for::

// within a function where form is an input = document.theform
for (x in form.func) // I also tried for (x in form["func"])
{
alert (x);
form.func[x].SelectedIndex = 0;
}

I put the alert in that loop for testing and it never seems to even
enter the loop.

Can this be done the way I want to do it?

Thanks in Advance for any help.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Jul 22 '06 #1
7 3782
I goofed the Subject. It should actually be

Accessing *select* form fields name=field[label]
(select, not text fields)

Chuck Anderson wrote:
I'm pretty much a JavaScript novice. I'm good at learning by example
and changing those examples to suit my needs. That said ....

.... I have some select fields in a form I created for a database search
that I am unable to figure out how to access. (The search is
implemented in Php/MySQL.)

The user enters search values for: name, address1, city, .... etc.,
.... and for each of these they also select whether the search should
look for the field to be equal to, not equal to, Like, or Not Like the
value in the database.

So in the form I have:
<input type=text name=city>
in combination with
<select name=func[city]>
<option value='='>=</option>
<option value='!='>!=</option>
<option value='LIKE'>LIKE</option>
<option value='!='NOT LIKE>NOT LIKE</option>
</select>

For an example, this search could be to find an entry "WHERE city
$func[city]] $city. (Php - e.g., WHERE city = 'Denver'). This works
fine - in Php. I use similar combinations for name, address, etc., ...

Okay, here's the JavaScript problem. For the Reset action I have a
JavaScript function to Reset all the form fields. I want to Reset the
selected indexes for the "func" fields (func[name], func[city],
func[state], ...) using a loop that goes through the associative array,
"func." I cannot find the right syntax to address them as an array and
I am beginning to think that JavaScript does not see them as an array.
I could have just as well named them func_name, func_address, func_city,
.... (??)

I have already come up with a workaround. I loop though *every* form
element and if substring(0,5) = 'func[' I set the SelectedIndex for that
element to 0.

What I would like is a definitive answer. Am I correct that JavaScript
does not see func as an array, or is there a syntax for accessing it
that has so far totally eluded me.

I can access individual members of func like so:
alert (form["func[host_id]"].selectedIndex);
and that works, but I cannot figure out the correct syntax to loop
through the array with:

for (x in form.func)

Is it simply not possible? If it is possible, what is the correct
syntax for::

// within a function where form is an input = document.theform
for (x in form.func) // I also tried for (x in form["func"])
{
alert (x);
form.func[x].SelectedIndex = 0;
}

I put the alert in that loop for testing and it never seems to even
enter the loop.

Can this be done the way I want to do it?

Thanks in Advance for any help.


--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Everyone's journey should be different,
so that we all are enriched
in new and endless ways
*****************************
Jul 22 '06 #2
Chuck Anderson wrote:
I'm pretty much a JavaScript novice. I'm good at learning by example
and changing those examples to suit my needs. That said ....

.... I have some select fields in a form I created for a database search
that I am unable to figure out how to access. (The search is
implemented in Php/MySQL.)

The user enters search values for: name, address1, city, .... etc.,
.... and for each of these they also select whether the search should
look for the field to be equal to, not equal to, Like, or Not Like the
value in the database.

So in the form I have:
<input type=text name=city>
in combination with
<select name=func[city]>
You should always quote attribute values in HTML - though it's not
always necessary, it saves having to remember which characters to quote
- the '[' and ']' characters must be quoted.

"In certain cases, authors may specify the value of an attribute
without any quotation marks. The attribute value may only contain
letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45),
periods (ASCII decimal 46), underscores (ASCII decimal 95), and
colons (ASCII decimal 58). We recommend using quotation marks even
when it is possible to eliminate them."
<URL:http://www.w3.org/TR/html4/intro/sgmltut.html#attributes>

<option value='='>=</option>
<option value='!='>!=</option>
<option value='LIKE'>LIKE</option>
<option value='!='NOT LIKE>NOT LIKE</option>
You possibly meant:

<option value='NOT LIKE'>NOT LIKE</option>

</select>

For an example, this search could be to find an entry "WHERE city
$func[city]] $city. (Php - e.g., WHERE city = 'Denver'). This works
fine - in Php. I use similar combinations for name, address, etc., ...

Okay, here's the JavaScript problem. For the Reset action I have a
JavaScript function to Reset all the form fields. I want to Reset the
Why can't you use a plain reset button? No script required.

selected indexes for the "func" fields (func[name], func[city],
func[state], ...) using a loop that goes through the associative array,
"func." I cannot find the right syntax to address them as an array and
There is no 'associative array' func unless you have created it elsewhere.

I am beginning to think that JavaScript does not see them as an array.
No, it doesn't. However, the option elements of a select are held in a
collection called options - but they don't have a reset method.

I could have just as well named them func_name, func_address, func_city,
.... (??)

I have already come up with a workaround. I loop though *every* form
element and if substring(0,5) = 'func[' I set the SelectedIndex for that
element to 0.
The obvious solution is:

<input type="reset">
If you insist on using JavaScript then call the form's reset method:

document.forms['formName'].reset();

What I would like is a definitive answer. Am I correct that JavaScript
does not see func as an array, or is there a syntax for accessing it
that has so far totally eluded me.
See above.

I can access individual members of func like so:
alert (form["func[host_id]"].selectedIndex);
and that works, but I cannot figure out the correct syntax to loop
through the array with:

for (x in form.func)
Looping through the options is unnecessary. Say you have:

<form name="formA" action="">
<select name="selA">
<option selected>one
<option>two
<option>three
</select>
<input type="reset">
<input type="button" value="Reset using script"
onclick="this.form.reset();">
<input type="button" value="Select two"
onclick="this.form.elements['selA'].selectedIndex = 1;">
</form>

Clicking the reset button will reset the form. To do it with script,
click the "Reset using script" button.

To set option 1 (the second one) as selected, click the "Select two" button.

To loop through all the options - which seems pointless, but what the
heck :-) - do something like:

var options = document.forms['formA'].elements['selA'].options;
for (var i=0, len=options.length; i<len; i++){
alert('Option ' + i + ': ' + options[i].value)
}

[...]

--
Rob
Jul 22 '06 #3
RobG wrote:
[...]
To loop through all the options - which seems pointless, but what the
heck :-) - do something like:

var options = document.forms['formA'].elements['selA'].options;
for (var i=0, len=options.length; i<len; i++){
alert('Option ' + i + ': ' + options[i].value)
Sorry, IE won't like that - if an option has no value attribute, IE
wants the text explicitly:

alert('Option ' + i + ': ' + options[i].text)
--
Rob
Jul 22 '06 #4
RobG said the following on 7/21/2006 11:41 PM:
RobG wrote:
[...]
>To loop through all the options - which seems pointless, but what the
heck :-) - do something like:

var options = document.forms['formA'].elements['selA'].options;
for (var i=0, len=options.length; i<len; i++){
alert('Option ' + i + ': ' + options[i].value)

Sorry, IE won't like that - if an option has no value attribute, IE
wants the text explicitly:

alert('Option ' + i + ': ' + options[i].text)
I have never understood what a select list without values was good for.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 22 '06 #5
First off, thanks for your help (reply interspersed).

RobG wrote:
Chuck Anderson wrote:
>I'm pretty much a JavaScript novice. I'm good at learning by example
and changing those examples to suit my needs. That said ....

.... I have some select fields in a form I created for a database search
that I am unable to figure out how to access. (The search is
implemented in Php/MySQL.)

The user enters search values for: name, address1, city, .... etc.,
.... and for each of these they also select whether the search should
look for the field to be equal to, not equal to, Like, or Not Like the
value in the database.

So in the form I have:
<input type=text name=city>
in combination with
<select name=func[city]>

You should always quote attribute values in HTML - though it's not
always necessary, it saves having to remember which characters to quote
- the '[' and ']' characters must be quoted.

"In certain cases, authors may specify the value of an attribute
without any quotation marks. The attribute value may only contain
letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45),
periods (ASCII decimal 46), underscores (ASCII decimal 95), and
colons (ASCII decimal 58). We recommend using quotation marks even
when it is possible to eliminate them."
<URL:http://www.w3.org/TR/html4/intro/sgmltut.html#attributes>
Oh yeah. I am aware of this and choose not to quote strictly
alphanumeric attributes, ... and I almost always remember to quote any
attributes containing other characters (like % or [ or ] ). Somehow I
forgot about that in this case.
><option value='='>=</option>
<option value='!='>!=</option>
<option value='LIKE'>LIKE</option>
<option value='!='NOT LIKE>NOT LIKE</option>

You possibly meant:

<option value='NOT LIKE'>NOT LIKE</option>
Yep, just a mistype.
></select>

For an example, this search could be to find an entry "WHERE city
$func[city]] $city. (Php - e.g., WHERE city = 'Denver'). This works
fine - in Php. I use similar combinations for name, address, etc., ...

Okay, here's the JavaScript problem. For the Reset action I have a
JavaScript function to Reset all the form fields. I want to Reset the

Why can't you use a plain reset button? No script required.
When the user Submits the search, the search page reloads itself,
re-displays the search form with the search criteria displayed in the
form, and displays the search results under that. At that point I want
Reset to to set to set all text fields back to blank and the comparator
function fields back to '=', but what it does at this point is Reset
them to the values that were set when the page reloaded itself. I want
them to go back to blank, so I have a function do it.
>
>selected indexes for the "func" fields (func[name], func[city],
func[state], ...) using a loop that goes through the associative array,
"func." I cannot find the right syntax to address them as an array and

There is no 'associative array' func unless you have created it elsewhere.

>I am beginning to think that JavaScript does not see them as an array.

No, it doesn't.
That's the key point for me. I can quit beating my head against the
wall and use my "workaround."
However, the option elements of a select are held in a
collection called options - but they don't have a reset method.

>I can access individual members of func like so:
alert (form["func[host_id]"].selectedIndex);
and that works, but I cannot figure out the correct syntax to loop
through the array with:

for (x in form.func)

Looping through the options is unnecessary.
Correct. And I don't need or want to, either. Although you've shown me
one more thing about JavaScript and forms I did not know. .option.
<form name="formA" action="">
<select name="selA">
<option selected>one
<option>two
<option>three
</select>
<input type="reset">
<input type="button" value="Reset using script"
onclick="this.form.reset();">
<input type="button" value="Select two"
onclick="this.form.elements['selA'].selectedIndex = 1;">
</form>

Clicking the reset button will reset the form. To do it with script,
click the "Reset using script" button.

To set option 1 (the second one) as selected, click the "Select two" button.

To loop through all the options - which seems pointless, but what the
heck :-) - do something like:

var options = document.forms['formA'].elements['selA'].options;
for (var i=0, len=options.length; i<len; i++){
alert('Option ' + i + ': ' + options[i].value)
}

[...]


--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Jul 22 '06 #6
JRS: In article <44c197ef$0$23507$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 22 Jul 2006 13:12:38 remote, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auposted :
>
You should always quote attribute values in HTML - though it's not
always necessary, it saves having to remember which characters to quote
ISTM that it's very easy to remember that alphanumerics (A-Za-z0-9)
don't need quoting, and that ordinary numeric values (such as -99.5)
don't need quoting; and that URLs *should* be quoted.

That leads, AFAICS, to quoting everything that needs it, and not too
much else (I'm aware that what follows href= sometimes does not *need*
quoting).

When HTML is being written out from javascript strings, knowing that
simple cases do not need quoting saves having to remember to alternate
or escape the quotes you don't need.
IIRC, there is an argument that moving on from HTML to some other
acronym will mean adding those quotes; but it will mean many other
changes too, and an experienced RegExp user should be able to do the
vast bulk of the extra quoting with an editing tool.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 22 '06 #7
Chuck Anderson wrote:
First off, thanks for your help (reply interspersed).

RobG wrote:
>Chuck Anderson wrote:
[...]
>Why can't you use a plain reset button? No script required.
When the user Submits the search, the search page reloads itself,
re-displays the search form with the search criteria displayed in the
form, and displays the search results under that. At that point I want
Reset to to set to set all text fields back to blank and the comparator
function fields back to '=', but what it does at this point is Reset
them to the values that were set when the page reloaded itself. I want
them to go back to blank, so I have a function do it.
Then I guess using the form's reset method would be best. You can call
it using body/window onload or use a script element immediately below
the form.

The problem with onload is that the form may display un-reset for some
time before the page finishes loading, then it will reset.

[...]
>>I am beginning to think that JavaScript does not see them as an
array.

No, it doesn't.
That's the key point for me. I can quit beating my head against the
wall and use my "workaround."
I probably should have written "Yes, it doesn't". English is a strange
language. ;-)

[...]
--
Rob
Jul 23 '06 #8

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

Similar topics

3
by: Dooza | last post by:
Hi there, I was wondering if anyone had come across some javascript that would allow me to have a chart of say 20 music tracks, and be able to move each track up and down the chart using up/down...
9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
4
by: justin tyme | last post by:
Hello Experts! I would like to combine (which may not be the correct technical term) two text fields from the same table in a query. Specifically, text field A and text field B are both lists of...
1
by: MasterChief | last post by:
I am sending a page a query string called job_id. What I want to do is take the job_id that is passed look it up in a database and make the text in a label on the new page display a value of one of...
9
by: Brett_A | last post by:
I have a form where the first field is a dynamic drop-down that pulls from a db (Access). The fields associated with the query are task_id, task_name and task_rate. The field has the value of...
4
by: David Tay | last post by:
Is there a text field tag with a value parameter that I can refer to with Javascript besides the input tag? What I have been doing is using Ajax and PHP to pull data out of a database. Then with...
2
by: Frowning Freezer | last post by:
How can the function below be rewritten to prioritize form fields instead of other objects with the same name? For example I want getele("title") to retrieve the form field named "title" instead...
2
by: Moiseszaragoza | last post by:
I am not sure if this goes in here ot the ASP forum I have been trying to get some simple AJAX on my site but its not going so well I am not sure what my problems is what i am trying to do is...
1
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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,...
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.