Connecting Tech Pros Worldwide Forums | Help | Site Map

Trouble passing variable to function

Max
Guest
 
Posts: n/a
#1: Sep 20 '05
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




Evertjan.
Guest
 
Posts: n/a
#2: Sep 20 '05

re: Trouble passing variable to function


Max wrote on 20 sep 2005 in comp.lang.javascript:
[color=blue]
> n the head of doc I have:
>
> <script type="text/javascript">
>
> function radioenable(value) {[/color]

value is a reserved word, do not use it as a variable name
[color=blue]
> document.forms.search.elements.value.disabled=fals e;[/color]

You cannot disable the value of an element

try this:

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

[color=blue]
> }
> </script>
>
> In the body:
>
> <input type="radio" name="datetype" value="rangdate"
> onclick="radioenable(rangdates);"><b>Range of Dates</b>[/color]

What is rangdates ? A variable? A declared element?
[color=blue]
>
> Shouldn't value become a variable holding the value of 'rangdates'?[/color]

Why? value is a reserved word, not a variable name!
[color=blue]
>
> I keep getting the js error:
>
> Error: 'document.forms.search.elements.value' is null or not an object[/color]

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



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

Zoe Brown
Guest
 
Posts: n/a
#3: Sep 20 '05

re: Trouble passing variable to function



"Max" <max@max.com> wrote in message
news:LH_Xe.129860$R83.91008@fe04.news.easynews.com ...[color=blue]
> 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;[/color]

do you mean

document.bla...selected = false ?
[color=blue]
> }
> </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'?[/color]

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.
[color=blue]
> I keep getting the js error:
>
> Error: 'document.forms.search.elements.value' is null or not an object[/color]

Yes.


Max
Guest
 
Posts: n/a
#4: Sep 20 '05

re: Trouble passing variable to function



"Evertjan." <exjxw.hannivoort@interxnl.net> wrote in message
news:Xns96D7EAE5A78eejj99@194.109.133.242...[color=blue]
> Max wrote on 20 sep 2005 in comp.lang.javascript:
>[color=green]
>> n the head of doc I have:
>>
>> <script type="text/javascript">
>>
>> function radioenable(value) {[/color]
>
> value is a reserved word, do not use it as a variable name
>[color=green]
>> document.forms.search.elements.value.disabled=fals e;[/color]
>
> You cannot disable the value of an element[/color]

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>

[color=blue]
>
> try this:
>
> function radioenable(x) {
> document.forms.search.elements[x].disabled=false;
> }
>
>[color=green]
>> }
>> </script>
>>
>> In the body:
>>
>> <input type="radio" name="datetype" value="rangdate"
>> onclick="radioenable(rangdates);"><b>Range of Dates</b>[/color]
>[/color]

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.

[color=blue]
> What is rangdates ? A variable? A declared element?[/color]

See above.

[color=blue]
>[color=green]
>>
>> Shouldn't value become a variable holding the value of 'rangdates'?[/color]
>
> Why? value is a reserved word, not a variable name!
>[color=green]
>>
>> I keep getting the js error:
>>
>> Error: 'document.forms.search.elements.value' is null or not an object[/color]
>
> It is not an object: elements expext an element pointer/name
>
>
>
> --
> Evertjan.
> The Netherlands.
> (Replace all crosses with dots in my emailaddress)
>[/color]


Max
Guest
 
Posts: n/a
#5: Sep 20 '05

re: Trouble passing variable to function


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" <zoenaomibrown@N-O-S-P-A-A-Mtesco.net> wrote in message
news:w%_Xe.29370$Aa1.22535@newsfe5-gui.ntli.net...[color=blue]
>
> "Max" <max@max.com> wrote in message
> news:LH_Xe.129860$R83.91008@fe04.news.easynews.com ...[color=green]
>> 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;[/color]
>
> do you mean
>
> document.bla...selected = false ?
>[color=green]
>> }
>> </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'?[/color]
>
> 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.
>[color=green]
>> I keep getting the js error:
>>
>> Error: 'document.forms.search.elements.value' is null or not an object[/color]
>
> Yes.
>[/color]


Zoe Brown
Guest
 
Posts: n/a
#6: Sep 20 '05

re: Trouble passing variable to function



"Max" <max@max.com> wrote in message
news:Zg%Xe.130447$R83.19370@fe04.news.easynews.com ...[color=blue]
>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.[/color]

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.
[color=blue]
>
> 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.[/color]

Post the code please.


Max
Guest
 
Posts: n/a
#7: Sep 20 '05

re: Trouble passing variable to function


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" <zoenaomibrown@N-O-S-P-A-A-Mtesco.net> wrote in message
news:%k%Xe.8469$1A.8241@newsfe1-gui.ntli.net...[color=blue]
>
> "Max" <max@max.com> wrote in message
> news:Zg%Xe.130447$R83.19370@fe04.news.easynews.com ...[color=green]
>>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.[/color]
>
> 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.
>[color=green]
>>
>> 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.[/color]
>
> Post the code please.
>
>[/color]


Evertjan.
Guest
 
Posts: n/a
#8: Sep 20 '05

re: Trouble passing variable to function


Max wrote on 21 sep 2005 in comp.lang.javascript:
[color=blue]
> <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;[/color]

you need the [] form, because the names are strings
[color=blue]
> }
> </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>[/color]

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'
[color=blue]
> <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>[/color]

again the rangdates,timestamp are strings,
not variables and should be quoted,
[color=blue]
> <br>
> <select style="background-color:#EEE;width:90;"[/color]
name="rangdates">[color=blue]
> <option value=\"1d\">Past 2 days</option>[/color]

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

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)

Max
Guest
 
Posts: n/a
#9: Sep 21 '05

re: Trouble passing variable to function


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." <exjxw.hannivoort@interxnl.net> wrote in message
news:Xns96D8499BAA24eejj99@194.109.133.242...[color=blue]
> Max wrote on 21 sep 2005 in comp.lang.javascript:
>[color=green]
>> <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;[/color]
>
> you need the [] form, because the names are strings
>[color=green]
>> }
>> </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>[/color]
>
> 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'
>[color=green]
>> <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>[/color]
>
> again the rangdates,timestamp are strings,
> not variables and should be quoted,
>[color=green]
>> <br>
>> <select style="background-color:#EEE;width:90;"[/color]
> name="rangdates">[color=green]
>> <option value=\"1d\">Past 2 days</option>[/color]
>
> the backslashes have no function here and should be removed
> <option value="1d">
> [this is not javascript but simple html]
>[color=green]
>> <option value=\"3d\">Past 5 days</option>
>> <option value=\"5d\">Past week</option>
>> </select>
>>
>> </form>[/color]
> ....
>
> 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)
>[/color]


Evertjan.
Guest
 
Posts: n/a
#10: Sep 21 '05

re: Trouble passing variable to function


Max wrote on 21 sep 2005 in comp.lang.javascript:
[color=blue]
> "Evertjan." <exjxw.hannivoort@interxnl.net> wrote in message[color=green]
>> function radioenable(y,z) {
>> document.forms.search.elements[y].disabled=false;
>> document.forms.search.elements[z].disabled=true;
>> }[/color][/color]

[please do not toppost on usenet]
[color=blue]
> 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?[/color]

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;
[color=blue]
> 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.[/color]

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>

[color=blue]
> BTW, the reason I had the back slashes is because I pulled this code
> from a php doc and forgot to remove them.[/color]

OK

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

Closed Thread