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

enable - disable <select>.

P: n/a
I've this code:

function checkdate(FormSubmit){
alert(document.getElementById('Mois').value);
if(eval(document.getElementById('Mois'))>0 &&
eval(document.getElementById('Annee'))>0){
document.forms['FormSubmit'].getElementByID('SELECTCONSTRUCTOR').disabled
= false; //can't reach the control here
}
else
{
document.forms['FormSubmit'].getElementByID('SELECTCONSTRUCTOR').disabled
= true; //can't reach the control here
}
}
....

<form name="FormSubmit" method="post" action="">
....
<select name="Mois" onChange=checkdate(this.form);>
....
<select name="Annee" onChange=checkdate(this.form);>
....
<select name="SELECTCONSTRUCTOR" disabled>

Now, what I would like, is to enable the SELECTCONSTRUCTOR if month and year
are selected (I mean different to 0).

I can't enable or disable. In fact I can't reach the control in the
checkdate function...

How can I enable/disable SELECTCONSTRUCTOR when my 2 fields are selected ???

BoB
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Bob Bedford wrote:
I've this code:

function checkdate(FormSubmit){
alert(document.getElementById('Mois').value);
if(eval(document.getElementById('Mois'))>0 &&
eval(document.getElementById('Annee'))>0){
document.forms['FormSubmit'].getElementByID('SELECTCONSTRUCTOR').disabled
= false; //can't reach the control here
}
else
{
document.forms['FormSubmit'].getElementByID('SELECTCONSTRUCTOR').disabled
= true; //can't reach the control here
}
}
...

<form name="FormSubmit" method="post" action="">
...
<select name="Mois" onChange=checkdate(this.form);>
...
<select name="Annee" onChange=checkdate(this.form);>
...
<select name="SELECTCONSTRUCTOR" disabled>

Now, what I would like, is to enable the SELECTCONSTRUCTOR if month and year
are selected (I mean different to 0).

I can't enable or disable. In fact I can't reach the control in the
checkdate function...

How can I enable/disable SELECTCONSTRUCTOR when my 2 fields are selected ???


Forget about eval, you don't need it.
If you have two select elements in a form container then simply pass the
form to your function as you do e.g.
<select onchange="checkdate(this.form);"
and then in
function checkdate (formObject) {
var select1 = form.elements.Mois;
var select2 = form.elements.SELECTCONSTRUCTOR;
...
if (...) {
select2.disabled = false;
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
In article <3f*********************@news.sunrise.ch>, "Bob Bedford"
<be******@YouKnowWhatToDohotmail.com> writes:
I've this code:
http://www.jibbering.com/faq/#FAQ4_13

Will explain how to get a reference to a select list.
function checkdate(FormSubmit){
alert(document.getElementById('Mois').value);
if(eval(document.getElementById('Mois'))>0 &&
eval(document.getElementById('Annee'))>0){


http://www.jibbering.com/faq/#FAQ4_40

eval is not needed there, nor is the getElementById the most
efficient/cross-browser way to do that:

el1=document.forms['FormSubmit'].elements['Mois'];
value= +el1.options[el1.selectedIndex].value;
el2=document.forms['FormSubmit'].elements['Annee'];
value= +el2.options[el2.selectedIndex].value;

The + in the two lines converts the string value to a number.

if ( el1>0 && el2>0){
document.forms['FormSubmit'].elements['SELECTCONSTRUCTOR'].disabled=false;
}
else{
document.forms['FormSubmit'].elements['SELECTCONSTRUCTOR'].disabled=true;
}
--
Randy
Jul 20 '05 #3

P: n/a
Bob Bedford wrote on 18 Dec 2003 at Thu, 18 Dec 2003 15:41:46 GMT:
alert(document.getElementById('Mois').value);
if(eval(document.getElementById('Mois'))>0 &&
eval(document.getElementById('Annee'))>0){
document.forms['FormSubmit'].getElementByID('SELECTCONSTRUCTOR' <snip> document.forms['FormSubmit'].getElementByID('SELECTCONSTRUCTOR'


In addition to what Mr Honnen and Mr Hikks said, you would have
trouble with the lines of code above.

1) The latter two lines have the method name in incorrect case; the
'D' in 'ID' should be lowercase.
2) All of those lines have names as arguments. Names are *not* the
same as IDs, and any browser that returns the correct element is
broken (namely, IE but possibly others). The retrieval of named
elements is accomplished using document.getElementsByName(). Be
aware that this returns a collection returning all elements that
match that name.

As Mr Hikks said, the better way of accessing named form controls
within a form element is through the use of the elements collection:

document.forms['form name'].elements['element name']

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.