If textarea is NOT empty, activate 2 radio buttons

S

Syswatch

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
 
A

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>
 
S

Syswatch

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.
 
A

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.type=='text' || what.tagName=='textarea')
&& what.value=='') {
alert('field or window text '+what.name+' not filled');
what.focus();
what.select();
return false;
}
return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top