Gossamer Forum
Home : General : Perl Programming :

Javascript & Forms

Quote Reply
Javascript & Forms
Hi

I have a checkbox, and a drop down select menu.

I want the select box to be disabled (or blurred, or not functioning, or...) as default when page loads up.

When user clicks in checkbox, I want the select box to become active.

When user de-selects the checkbox I want the select box to become blurred again.

Has anyone got a javascript snippet that does? I can't find such an example anywhere, although I'm sure I've seen such an example at some sites?

Thanks!

- wil
Quote Reply
Re: [Wil] Javascript & Forms In reply to
Hi,

Quote:
I want the select box to be disabled (or blurred, or not functioning, or...) as default when page loads up.

<select name=obj_select disabled>
<option>select 1
</select>
<input name=obj_checkbox onclick="switch(this)"

<script>
function this(obj) {
if (obj.checked)
document.myform.obj_select.disabled = false;
else
document.myform.obj_select.disabled = true
}
</script>

Hope that helps.

Cheers,
TheStone.


B.

Last edited by:

TheStone: Dec 3, 2001, 9:54 AM
Quote Reply
Re: [TheStone] Javascript & Forms In reply to
Thanks. I'll try and implement this and see how far I get.

- wil
Quote Reply
Re: [TheStone] Javascript & Forms In reply to
Thanks for your help. I've now got this, which doesn't want to work.

Code:
<script language="JavaScript">
<!--
function this(obj) {
if (obj.checked)
document.form_add.obj_select.disabled = false;
else
document.form_add.obj_select.disabled = true;
}
//-->
</script>

...

<FORM ACTION="odb.cgi" METHOD="POST" NAME="form_add">

...

<select name="$db_col_value" style="font-size:11px" disabled>

...

<input type="checkbox" name="$rd_bt_key_no_no" value="$rd_bt_and_vls{$rd_bt_key}" OnClick="switch(this)"> $rd_bt_and_vls{$rd_bt_key}

When I open the page the select box is blank, but when I click on the checkbox nothing happens. Any ideas?

Cheers

- wil
Quote Reply
Re: [Wil] Javascript & Forms In reply to
I'm guessing this should actually be:

Code:
<script language="JavaScript">
<!--
function this(obj) {
if (obj.checked) {
document.form_add.obj_select.disabled = false; }
else {
document.form_add.obj_select.disabled = true; }
}
//-->
</script>

But now I am getting JS error "Expected '{'" ?

- wil
Quote Reply
Re: [Wil] Javascript & Forms In reply to
Um no the { } aren't needed with JS Crazy

I think you want the function name to be switch() not this()
Quote Reply
Re: [PaulW] Javascript & Forms In reply to
No. Still getting the "expected {" error at line:

<input type="checkbox" name="$rd_bt_key_no_no" value="$rd_bt_and_vls{$rd_bt_key}" OnClick="switch(this)"> $rd_bt_and_vls{$rd_bt_key}

- wil
Quote Reply
Re: [Wil] Javascript & Forms In reply to
Could be the perl hash screwing it up.

Quote Reply
Re: [PaulW] Javascript & Forms In reply to
Hmm. I removed all coding and just left the function there at the top of the HTML document.

And it says I'm missing a (. Hmm.

It could be the template parser I thought at first, but it's not as the code outputs fine in my browser. Well outputs fine, but it's not working <g>.

- wil
Quote Reply
Re: [Wil] Javascript & Forms In reply to
My limited javascript experience has been that if you start randomly throwing in semi-colons ";" then the expected ( or { will go away. (seriously). Okay, maybe not randomly, but you get the idea.
Quote Reply
Re: [Wil] Javascript & Forms In reply to
Hi,

Because it could not find "obj_select" object in your form, so you have to name the drop down is "obj_select" or whatever, it should be:

<select name="whatever" style="font-size:11px" disabled>
...
</select>
and then in js script:
if (obj.checked)
document.form_add.whatever.disabled = false;
else
document.form_add.whatever.disabled = true;

It should work,

Cheers,
TheStone.

B.

Last edited by:

TheStone: Dec 4, 2001, 9:36 AM
Quote Reply
Re: [TheStone] Javascript & Forms In reply to
But I've got a problem with this line:

function this(obj) {

'this' is obviously some kind of reserved word. When I use the above line the page outputs a JS error on that line. If I change 'this' to 'something' then the page parses OK.

What is the function 'this' and how do I put it?

- wil
Quote Reply
Re: [Wil] Javascript & Forms In reply to
Hi,

Sorry about that, it should be

function switch(obj) {
....
}

Cheers,
TheStone.

B.
Quote Reply
Re: [TheStone] Javascript & Forms In reply to
The following code still thrown an error :-\ (using IE6):

Code:
<script language="javascript">
<!--
function switch(obj) {
if (obj.checked)
document.form_add.res_places_sl.disabled = false;
else
document.form_add.res_places_sl.disabled = true;
}
-->
</script>

The error is:

Expected (

?

Thanks!

- wil

Last edited by:

Wil: Dec 4, 2001, 11:10 AM
Quote Reply
Re: [Wil] Javascript & Forms In reply to
Can you post your html script



B.
Quote Reply
Re: [Wil] Javascript & Forms In reply to
Hi Wil,

You can't use switch as a function name. You will need to rename that function to something like my_switch and use the switch statement inside of your function.

Code:
function my_switch(obj) {
switch(obj)
{
case "1":
...;
break;

case "2":
...;

default:
...;
break;
}
}

Regards,
Charlie
Quote Reply
Re: [CP] Javascript & Forms In reply to
Yeah, I thought as much, after reading the docs on Netscape's developer site.

Hmm. Here goes.

Code:
<html>
<head>

<script language="javascript">
<!--
function switch(obj) {
if (obj.checked)
document.form_add.foo2.disabled = false;
else
document.form_add.foo2.disabled = true;
}
-->
</script>

</head>

<body>

<form name="form_add">

<input type="checkbox" onclick="switch(this)" name="foo" value="bar">
<select name="foo2" disabled>
<option></option>
</select>

</body>
</html>

Rgds


- wil