473,396 Members | 2,158 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,396 software developers and data experts.

Trouble passing variable to function

Max
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 type="text/javascript">

function radioenable(value) {

document.forms.search.elements.value.disabled=fals e;
}
</script>

In the body:

<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates);"><b>Range of Dates</b>

Shouldn't value become a variable holding the value of 'rangdates'?

I keep getting the js error:

Error: 'document.forms.search.elements.value' is null or not an object

Also, should I be able to use 'this.form' instead of the form name of
'search'?

Thanks,
Max

Sep 20 '05 #1
9 2131
Max wrote on 20 sep 2005 in comp.lang.javascript:
n the head of doc I have:

<script type="text/javascript">

function radioenable(value) {
value is a reserved word, do not use it as a variable name
document.forms.search.elements.value.disabled=fals e;
You cannot disable the value of an element

try this:

function radioenable(x) {
document.forms.search.elements[x].disabled=false;
}

}
</script>

In the body:

<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates);"><b>Range of Dates</b>
What is rangdates ? A variable? A declared element?

Shouldn't value become a variable holding the value of 'rangdates'?
Why? value is a reserved word, not a variable name!

I keep getting the js error:

Error: 'document.forms.search.elements.value' is null or not an object


It is not an object: elements expext an element pointer/name

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 20 '05 #2

"Max" <ma*@max.com> wrote in message
news:LH********************@fe04.news.easynews.com ...
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 type="text/javascript">

function radioenable(value) {

document.forms.search.elements.value.disabled=fals e;
do you mean

document.bla...selected = false ?
}
</script>

In the body:

<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates);"><b>Range of Dates</b>

Shouldn't value become a variable holding the value of 'rangdates'?
No, you have made some mistakes. What is rangdates and what are you trying
to do ?

a radio button can be selected or not. You can assign a value to it and
choose to use this if you wish.
I keep getting the js error:

Error: 'document.forms.search.elements.value' is null or not an object


Yes.
Sep 20 '05 #3
Max

"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn******************@194.109.133.242...
Max wrote on 20 sep 2005 in comp.lang.javascript:
n the head of doc I have:

<script type="text/javascript">

function radioenable(value) {
value is a reserved word, do not use it as a variable name
document.forms.search.elements.value.disabled=fals e;


You cannot disable the value of an element


I just chose value as the name of the variable for this post, I've actually
tried one charactor names...rangdates is the name of a drop down select
element:

<select style="background-color:#EEE;width:90;" name="rangdates">
<option value=\"1d\">Past 2 days</option>
<option value=\"3d\">Past 5 days</option>
<option value=\"5d\">Past week</option>
</select>


try this:

function radioenable(x) {
document.forms.search.elements[x].disabled=false;
}

}
</script>

In the body:

<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates);"><b>Range of Dates</b>


It still is not working. Now I'm getting the following error:

Error: 'document.forms.search.elements[...]' is null or not an object.

It works fine if I put the complete statement in the function without the
variable, but it doesn't seem to pass the variable.

What is rangdates ? A variable? A declared element?
See above.


Shouldn't value become a variable holding the value of 'rangdates'?


Why? value is a reserved word, not a variable name!

I keep getting the js error:

Error: 'document.forms.search.elements.value' is null or not an object


It is not an object: elements expext an element pointer/name

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 20 '05 #4
Max
I guess I made a mistake by not posting what I'm trying to do.

I have two radio buttons, a text input box, and a drop down select box.

When the page is loaded, the text box is enabled the drop down box element
is disabled (via javascript).

What I'm trying to do is when the 2nd radio is clicked, the input box is
disabled and the drop down box is enabled, then if the first radio is
clicked, the opposite.

It all works fine with javascript inline, or if I create to separate
javascript functions and don't try to pass any variables.

Thanks,
Max

"Zoe Brown" <zo***********@N-O-S-P-A-A-Mtesco.net> wrote in message
news:w%*******************@newsfe5-gui.ntli.net...

"Max" <ma*@max.com> wrote in message
news:LH********************@fe04.news.easynews.com ...
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 type="text/javascript">

function radioenable(value) {

document.forms.search.elements.value.disabled=fals e;


do you mean

document.bla...selected = false ?
}
</script>

In the body:

<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates);"><b>Range of Dates</b>

Shouldn't value become a variable holding the value of 'rangdates'?


No, you have made some mistakes. What is rangdates and what are you
trying to do ?

a radio button can be selected or not. You can assign a value to it and
choose to use this if you wish.
I keep getting the js error:

Error: 'document.forms.search.elements.value' is null or not an object


Yes.

Sep 20 '05 #5

"Max" <ma*@max.com> wrote in message
news:Zg********************@fe04.news.easynews.com ...
I guess I made a mistake by not posting what I'm trying to do.

I have two radio buttons, a text input box, and a drop down select box.
eh ?? a radio botton is a small circle that you can select or not, it is NOT
a text input box or a select box.

When the page is loaded, the text box is enabled the drop down box element
is disabled (via javascript).

What I'm trying to do is when the 2nd radio is clicked, the input box is
disabled and the drop down box is enabled, then if the first radio is
clicked, the opposite.

It all works fine with javascript inline, or if I create to separate
javascript functions and don't try to pass any variables.


Post the code please.
Sep 20 '05 #6
Max
You misunderstood me again. I've rewritten the code isolated in a new html
doc and tested it. It still does not work, so either the variables are not
passing, or I do not understand how to reference the variable once it's been
passed. I comented out the 2nd method of capturing the variables as the
first responder to this post suggested, but it still didn't work.

<html>
<head>
<script type="text/javascript">
function radioenable(y,z) {
document.forms.search.elements.y.disabled=false;
//document.forms.search.elements[y].disabled=false;
document.forms.search.elements.z.disabled=true;
//document.forms.search.elements[z].disabled=true;
}
</script>
</head>
<body>

<form name="search" action="list.php" method="post">
<input type="radio" name="datetype" value="spdate"
onclick="radioenable(timestamp,rangdates);"checked ><b>Specific Date</b>
<br>
<input type="text" name="timestamp">
<br><br>
<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates,timestamp);"><b>Ran ge of Dates</b>
<br>
<select style="background-color:#EEE;width:90;" name="rangdates">
<option value=\"1d\">Past 2 days</option>
<option value=\"3d\">Past 5 days</option>
<option value=\"5d\">Past week</option>
</select>

</form>

//Disable drop down box as soon as page is loaded
<script
type="text/javascript">document.forms.search.elements.rangdat es.disabled=true;</script>

</body>
</html>
Thanks,
Max
"Zoe Brown" <zo***********@N-O-S-P-A-A-Mtesco.net> wrote in message
news:%k****************@newsfe1-gui.ntli.net...

"Max" <ma*@max.com> wrote in message
news:Zg********************@fe04.news.easynews.com ...
I guess I made a mistake by not posting what I'm trying to do.

I have two radio buttons, a text input box, and a drop down select box.


eh ?? a radio botton is a small circle that you can select or not, it is
NOT a text input box or a select box.

When the page is loaded, the text box is enabled the drop down box
element is disabled (via javascript).

What I'm trying to do is when the 2nd radio is clicked, the input box is
disabled and the drop down box is enabled, then if the first radio is
clicked, the opposite.

It all works fine with javascript inline, or if I create to separate
javascript functions and don't try to pass any variables.


Post the code please.

Sep 20 '05 #7
Max wrote on 21 sep 2005 in comp.lang.javascript:
<html>
<head>
<script type="text/javascript">
function radioenable(y,z) {
document.forms.search.elements.y.disabled=false;
//document.forms.search.elements[y].disabled=false;
document.forms.search.elements.z.disabled=true;
//document.forms.search.elements[z].disabled=true;
you need the [] form, because the names are strings
}
</script>
</head>
<body>

<form name="search" action="list.php" method="post">
<input type="radio" name="datetype" value="spdate"
onclick="radioenable(timestamp,rangdates);"checked ><b>Specific Date</b>
the rangdates,timestamp are strings, not variables and should be quoted,
[single quoted, because the double quote is used for the outher quote]
'rangdates','timestamp'
<br>
<input type="text" name="timestamp">
<br><br>
<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates,timestamp);"><b>Ran ge of Dates</b>
again the rangdates,timestamp are strings,
not variables and should be quoted,
<br>
<select style="background-color:#EEE;width:90;" name="rangdates"> <option value=\"1d\">Past 2 days</option>
the backslashes have no function here and should be removed
<option value="1d">
[this is not javascript but simple html]
<option value=\"3d\">Past 5 days</option>
<option value=\"5d\">Past week</option>
</select>

</form>

.....

The below works correct.
I removed your working onload script in favor of a
<select disabled>

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

<html>
<head>
<script type="text/javascript">
function radioenable(y,z) {
document.forms.search.elements[y].disabled=false;
document.forms.search.elements[z].disabled=true;
}
</script>
</head>
<body>

<form name="search" action="list.php" method="post">
<input type="radio" name="datetype" value="spdate"
onclick="radioenable('timestamp','rangdates');" checked><b>Specific Date
</b>
<br>
<input type="text" name="timestamp">
<br><br>
<input type="radio" name="datetype" value="rangdate"
onclick="radioenable('rangdates','timestamp');"><b >Range of Dates</b>
<br>
<select style="background-color:#EEE;width:90;" name="rangdates"
disabled>
<option value="1d">Past 2 days</option>
<option value="3d">Past 5 days</option>
<option value="5d">Past week</option>
</select>
</form>
</body>
</html>

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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 20 '05 #8
Max
That works great. Thanks for help, I now understand how to pass variables.

A couple of question if you don't mind.

1) Why is the dot needed on one side of the brackets and not the other?
2) How would I use this.form, or how is it used? I guess to use in different
forms, I could just add an extra variable for the function.

BTW, the reason I had the back slashes is because I pulled this code from a
php doc and forgot to remove them.

Thanks again.
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn*******************@194.109.133.242...
Max wrote on 21 sep 2005 in comp.lang.javascript:
<html>
<head>
<script type="text/javascript">
function radioenable(y,z) {
document.forms.search.elements.y.disabled=false;
//document.forms.search.elements[y].disabled=false;
document.forms.search.elements.z.disabled=true;
//document.forms.search.elements[z].disabled=true;


you need the [] form, because the names are strings
}
</script>
</head>
<body>

<form name="search" action="list.php" method="post">
<input type="radio" name="datetype" value="spdate"
onclick="radioenable(timestamp,rangdates);"checked ><b>Specific Date</b>


the rangdates,timestamp are strings, not variables and should be quoted,
[single quoted, because the double quote is used for the outher quote]
'rangdates','timestamp'
<br>
<input type="text" name="timestamp">
<br><br>
<input type="radio" name="datetype" value="rangdate"
onclick="radioenable(rangdates,timestamp);"><b>Ran ge of Dates</b>


again the rangdates,timestamp are strings,
not variables and should be quoted,
<br>
<select style="background-color:#EEE;width:90;"

name="rangdates">
<option value=\"1d\">Past 2 days</option>


the backslashes have no function here and should be removed
<option value="1d">
[this is not javascript but simple html]
<option value=\"3d\">Past 5 days</option>
<option value=\"5d\">Past week</option>
</select>

</form>

....

The below works correct.
I removed your working onload script in favor of a
<select disabled>

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

<html>
<head>
<script type="text/javascript">
function radioenable(y,z) {
document.forms.search.elements[y].disabled=false;
document.forms.search.elements[z].disabled=true;
}
</script>
</head>
<body>

<form name="search" action="list.php" method="post">
<input type="radio" name="datetype" value="spdate"
onclick="radioenable('timestamp','rangdates');" checked><b>Specific Date
</b>
<br>
<input type="text" name="timestamp">
<br><br>
<input type="radio" name="datetype" value="rangdate"
onclick="radioenable('rangdates','timestamp');"><b >Range of Dates</b>
<br>
<select style="background-color:#EEE;width:90;" name="rangdates"
disabled>
<option value="1d">Past 2 days</option>
<option value="3d">Past 5 days</option>
<option value="5d">Past week</option>
</select>
</form>
</body>
</html>

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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 20 '05 #9
Max wrote on 21 sep 2005 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in message
function radioenable(y,z) {
document.forms.search.elements[y].disabled=false;
document.forms.search.elements[z].disabled=true;
}

[please do not toppost on usenet]
That works great. Thanks for help, I now understand how to pass
variables.

A couple of question if you don't mind.

1) Why is the dot needed on one side of the brackets and not the
other?
because in elements[y] y is a string parameter of elements

Legal is:
var s = 'stamp'
var a = document.forms.search.elements['time'+s]
a.disabled=false;
2) How would I use this.form, or how is it used? I guess to use
in different forms, I could just add an extra variable for the
function.
Sorry, perhaps I don't understand your question.
Something like this? Try:

<html>
<head>
<script type="text/javascript">
function alertHidden(x){
alert(x.firstChild.value)
}
</script>
</head>
<body>
<form onsubmit='alertHidden(this);return false;'>
<input type='hidden' value='ha'>
<input type='submit'>
</form>
<form onsubmit='alertHidden(this);return false;'>
<input type='hidden' value='Krrrrr'>
<input type='submit'>
</form>
</body>
</html>

BTW, the reason I had the back slashes is because I pulled this code
from a php doc and forgot to remove them.


OK

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 21 '05 #10

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
8
sonic
by: sonic | last post by:
I am having trouble passing two variable types into my printPattern function. I need to pass rows and characterSelect from my getInput function into my printPattern function. I keep getting a...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...

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.