470,631 Members | 2,158 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

If textarea is NOT empty, activate 2 radio buttons

Hi out there...
I am pretty new to javascript, and is looking for some help here.

I have a problem, I need a small javascript, which checks if there is
any text in my textarea, and if it is NOT empty it should activate 2
radio buttons below.
It also has to check that one of these radio buttons is checked if they
are active.

Can some help me out here....
I can find lots of examples on the opposite :-(

Best regards,
Jesper Andersen

Dec 9 '06 #1
3 8062
ASM
Syswatch a écrit :
I have a problem, I need a small javascript, which checks if there is
any text in my textarea, and if it is NOT empty it should activate 2
radio buttons below.
It also has to check that one of these radio buttons is checked if they
are active.

soluce 1:

<textarea name="comment"
onchange="
document.getElementById('rad').style.display = this.value==''?
'none' : 'block';
if(this.form.comment_choice[0].checked) alert('yes');
else
if(this.form.comment_choice[1].checked) alert('no');
else alert('choice bellow');"></textarea>
<p id="rad" style="display:none">
yes ? <input type=radio name="comment_choice" value="yes">
no ? <input type=radio name="comment_choice" value="no">
</p>
soluce 2 :

<textarea name="comment"
onchange="
if(this.value=='') {
this.form.comment_choice[0].disabled = true;
this.form.comment_choice[1].disabled = true;
}
else {
this.form.comment_choice[0].disabled = false;
this.form.comment_choice[1].disabled = false;
}
if(this.form.comment_choice[0].checked) alert('yes');
else
if(this.form.comment_choice[1].checked) alert('no');
else
if(this.value!='') alert('choice bellow');"></textarea>
<br>
yes ? <input type=radio name="comment_choice" value="yes" disabled>
no ? <input type=radio name="comment_choice" value="no" disabled>
soluce 3 :

<textarea name="comment"
onchange="
var R1 = this.form.comment_choice[0];
var R2 = this.form.comment_choice[1];
if(this.value=='') {
R1.disabled = true;
R2.disabled = true;
}
else {
R1.disabled = false;
R2.disabled = false;
if(R1.checked) alert('yes');
else
if(R2.checked) alert('no');
else
alert('choice bellow');
}"></textarea>
<br>
yes ? <input type=radio name="comment_choice" value="yes" disabled>
no ? <input type=radio name="comment_choice" value="no" disabled>
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 9 '06 #2
It looks good, but there is just a small bug.
When the radio buttons are activated, you can push submit again without
any prompt that you need to pick one.

Dec 9 '06 #3
ASM
Syswatch a écrit :
It looks good, but there is just a small bug.
When the radio buttons are activated, you can push submit again without
any prompt that you need to pick one.

It will always have something wrong what about you want this or that ...
You did ask for textarea, no ?

For submitting you need another function to verify all fields and
element of form (not only textarea and its little cousins)

<form onsubmit="return verif(this)" ... >

JS :
====
function verif(what) {
what = what.elements;
for(var i=0; i<what.length; i++) {
if((what[i].type=='text' || what[i].tagName=='textarea')
&& what[i].value=='') {
alert('field or window text '+what.name+' not filled');
what[i].focus();
what[i].select();
return false;
}
return true;
}
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Suzanne | last post: by
2 posts views Thread by dpazza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.