473,378 Members | 1,343 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,378 software developers and data experts.

Passing input names to javascript fuction.

I have an array for of input names. Input1 , input 2 etc. Can I pass
the input names in a loop like

document.all[input].value="xxxx"

The problem is document.all cannot be used in Safari/firefox, I tried
using document.getElementById but my inputs do have any IDs. is there
any way of accomplishing thsse.?

Oct 10 '06 #1
6 1744
wrote on 10 okt 2006 in comp.lang.javascript:
I have an array for of input names. Input1 , input 2 etc. Can I pass
the input names in a loop like

document.all[input].value="xxxx"

The problem is document.all cannot be used in Safari/firefox, I tried
using document.getElementById but my inputs do have any IDs. is there
any way of accomplishing thsse.?
document.getElementById(inputID)

will work, if you give the inputs an individual id.

However, maybe you like this:

=========================
<input><br>
<input><br>
<input><br>
<input><br>
<input><br>

<script type='text/javascript'>

var inputArray = document.getElementsByTagName("input");
var j = 7;
for (var i in inputArray)
inputArray[i].value = 'Number: ' + j++;

</script>
=========================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 10 '06 #2

Evertjan. wrote:
wrote on 10 okt 2006 in comp.lang.javascript:
I have an array for of input names. Input1 , input 2 etc. Can I pass
the input names in a loop like

document.all[input].value="xxxx"

The problem is document.all cannot be used in Safari/firefox, I tried
using document.getElementById but my inputs do have any IDs. is there
any way of accomplishing thsse.?

document.getElementById(inputID)

will work, if you give the inputs an individual id.

However, maybe you like this:

=========================
<input><br>
<input><br>
<input><br>
<input><br>
<input><br>

<script type='text/javascript'>

var inputArray = document.getElementsByTagName("input");
var j = 7;
for (var i in inputArray)
inputArray[i].value = 'Number: ' + j++;

</script>
=========================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Thanks. That's new to me but unfortunately very difficult to implement
as my function generates the input name and it checks the input
repeatedly. My code looks like this at the moment

function processBackground(){ for (n=1;n<11;n++)
{
cellrow="r"+n
for (i=1;i<17;i++){
cell=cellrow+"w"+i
cellName=cell+"_ID"
cellID=cell+"ID"
if (document.all[cell].value=="X"){
document.getElementById(cellName).style.background Color='#84DFC1'
}
}
}
}

I need to change that line with the if statement

Any ideas?

Oct 10 '06 #3
wrote on 10 okt 2006 in comp.lang.javascript:
>>
<script type='text/javascript'>

var inputArray = document.getElementsByTagName("input");
var j = 7;
for (var i in inputArray)
inputArray[i].value = 'Number: ' + j++;

</script>
=========================
Thanks. That's new to me but unfortunately very difficult to implement
as my function generates the input name and it checks the input
repeatedly. My code looks like this at the moment

function processBackground(){ for (n=1;n<11;n++)
{
cellrow="r"+n
for (i=1;i<17;i++){
cell=cellrow+"w"+i
cellName=cell+"_ID"
cellID=cell+"ID"
if (document.all[cell].value=="X"){
document.getElementById(cellName).style.background Color='#84DFC1'
}
}
}
}

I need to change that line with the if statement
You now seem to come with a table "cell"?,
you did not mention before.
A table cell is not an input field.

If it is not a table cell, do not use reserved words, you are bount to
get into trouble.

My advice is forget about the outdated document.all, if you want cross
browser compatibility, and start over again.

If you set your element id's this way, it probably is just as simple to
access those elements directly.

Do some decent indenting, proper var-ing, systemized capitalisation:

function processBackground(){
var cell, cellRow, cellName, cellID;
for (var n=1 ; n<11 ; n++) {
cellRow="r"+n;
for (var i=1 ; i<17 ; i++){
cell = cellRow + "w" + i;
cellName = cell + "_ID";
cellID = cell + "ID";
if (document.all[cell].value == "X"){
document.getElementById(cellName).style.background Color
= '#84DFC1';
};
};
};
};

And change it to [if we are still talking <input>]:

function processBackground(myElement){
var arr = myElement.getElementsByTagName("input");
for (var i in arr)
if (arr[i].value == "X")
arr[i].style.backgroundColor = '#84DFC1';
};

if you want to execute this for all <inputin the document, run it as:

processBackground(document);

or for a part:

processBackground(document.getElementById('myConta inerDiv'));

Not tested.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 10 '06 #4

Evertjan. wrote:
wrote on 10 okt 2006 in comp.lang.javascript:
>
<script type='text/javascript'>

var inputArray = document.getElementsByTagName("input");
var j = 7;
for (var i in inputArray)
inputArray[i].value = 'Number: ' + j++;

</script>
=========================
Thanks. That's new to me but unfortunately very difficult to implement
as my function generates the input name and it checks the input
repeatedly. My code looks like this at the moment

function processBackground(){ for (n=1;n<11;n++)
{
cellrow="r"+n
for (i=1;i<17;i++){
cell=cellrow+"w"+i
cellName=cell+"_ID"
cellID=cell+"ID"
if (document.all[cell].value=="X"){
document.getElementById(cellName).style.background Color='#84DFC1'
}
}
}
}

I need to change that line with the if statement

You now seem to come with a table "cell"?,
you did not mention before.
A table cell is not an input field.

If it is not a table cell, do not use reserved words, you are bount to
get into trouble.

My advice is forget about the outdated document.all, if you want cross
browser compatibility, and start over again.

If you set your element id's this way, it probably is just as simple to
access those elements directly.

Do some decent indenting, proper var-ing, systemized capitalisation:

function processBackground(){
var cell, cellRow, cellName, cellID;
for (var n=1 ; n<11 ; n++) {
cellRow="r"+n;
for (var i=1 ; i<17 ; i++){
cell = cellRow + "w" + i;
cellName = cell + "_ID";
cellID = cell + "ID";
if (document.all[cell].value == "X"){
document.getElementById(cellName).style.background Color
= '#84DFC1';
};
};
};
};

And change it to [if we are still talking <input>]:

function processBackground(myElement){
var arr = myElement.getElementsByTagName("input");
for (var i in arr)
if (arr[i].value == "X")
arr[i].style.backgroundColor = '#84DFC1';
};

if you want to execute this for all <inputin the document, run it as:

processBackground(document);

or for a part:

processBackground(document.getElementById('myConta inerDiv'));

Not tested.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Thanks I tried the getElementsbyId before posting the last one and it
didnt work maybe I will try again. Anyway, I had fields in a table. I
change the colour of the table cells based on the value in the field.

Oct 11 '06 #5

ef*****@epitome.com.sg wrote:
ef*****@epitome.com.sg wrote:
Thanks. That's new to me but unfortunately very difficult to implement
as my function generates the input name and it checks the input
repeatedly. My code looks like this at the moment
>
function processBackground(){ for (n=1;n<11;n++)
{
cellrow="r"+n
for (i=1;i<17;i++){
cell=cellrow+"w"+i
cellName=cell+"_ID"
cellID=cell+"ID"
Is there supposed to be an underscore there?

cellID=cell+"_ID"

if (document.all[cell].value=="X"){
document.getElementById(cellName).style.background Color='#84DFC1'
Here you are using the variable 'cellName' for what should be an ID
attribute, yet your code above seems to set the name and ID with
different values - or is the missing underscore an error?

[...]
Thanks I tried the getElementsbyId before posting the last one and it
didnt work maybe I will try again. Anyway, I had fields in a table. I
change the colour of the table cells based on the value in the field.
Then you should probably be using onchange (and therefore don't need to
know the input's id or name) rather than continuously checking the
values of the fields.

Otherwise, as suggested by Evertjan, get references to the inputs once
and store them in an array or object. Then you just iterate over the
array each time you want to check the inputs - it is much faster and
more efficient than re-generating IDs and using getElementById every
time.
--
Rob

Oct 11 '06 #6
wrote on 11 okt 2006 in comp.lang.javascript:
Thanks I tried the getElementsbyId before posting the last one and it
didnt work maybe I will try again.
Please stop saying "didn't work", without explaining what happenend and if
an error [what error?] was generated, and a piese of code, showing the
problem. We in this NG cannot help "didn't work".
Anyway, I had fields in a table. I
change the colour of the table cells based on the value in the field.
A <td>...</tdhas no "value" but an "innerHTML".
Do not use those terms loosely.

In a table, you can use "cells"

Be advised that testing on the whole content of a <td>
can give unexpected results with extra spaces,
so use a regex //.test(), much more flexible:

============

<table id='myTable' border=1><tr>
<td>A</td>
<td>X</td>
<td>A</td>
<td>Fax</td>
</tr></table>

<script type='text/javascript'>

function processBackground(myElement){
var arr = myElement.cells;
for (var i in arr)
if (/x/i.test(arr[i].innerHTML))
arr[i].style.backgroundColor = '#84DFC1';
};

processBackground(document.getElementById('myTable '));
// processBackground(document.getElementById('myOther Table'));

</script>

==============

IE6 tested

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 11 '06 #7

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

Similar topics

3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
7
by: Dave A | last post by:
I have two events in a form that I am passing. One is to a javascript function, the other is to the same .asp page only with another action to show different data. Onchange is calls the...
6
by: Eric Johnston | last post by:
I want the visitor to enter three numbers on the page and then click a button "generate image" which will I hope cause a generated gif image to be displayed alongside on the page. This involved...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
1
by: | last post by:
Hi, 1st, I did a search and could not find any info on this, the Google results were good, but I'm still have issues...So any help will be great. I have a frame page, which contains 3 frames...
1
by: Marty Meyers | last post by:
TIA for your help! I have a page (code snip below) named "browse" which has a table in it with rows like this: 2003 1500 Silverado pickup red 51,024 $19,995 Details --------- each row...
0
by: kencana | last post by:
hi All, I got problem in passing data (more than one) from soap client to the soap server. but if i only passing one data at a time, it works successfully.. The following is the error message i...
10
by: patelxxx | last post by:
Problem: I am trying to input my name in a text box and I click on transfer and I want the name to appear on the same page (i.e. in another form). How is this done? <BODY> <HTML> <form...
1
by: chas2007 | last post by:
I need to pass a variable from a php function to a form. Does anyone have an example of code that would do this? Below is the code. I need to pass the "return value" from the php function mem_cost...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.