By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,694 Members | 2,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,694 IT Pros & Developers. It's quick & easy.

Trouble passing variable to function

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.