PDA

View Full Version : Generic Form reiteration to find focused textarea..HOW



BrightNail
Aug 13th, 2002, 08:59 PM
hey all...

some of my pages have multiple forms...., others don't..so I want to read into the form object..the number of forms..THEN reiterate thru them to see if a textarea WITHIN these forms (if it exists)..has focus....

I'm stuck....this is what i have so far..

var x = document.forms.length
for (p=0;p<x;p++){
if(document.forms[p].elements.textarea){alert("textarea focused")}
}

but doesn't work,,,I'm brain farting...as I am not very versed in form objects...cause I usually always use "named "objects..here, it is generic..

please help

glenngv
Aug 14th, 2002, 04:40 AM
this is partially what you want. please explain more why do you want to know if a particular textarea has focus.

for (var i=0;i<document.forms.length;i++){
for (var j=0;j<document.forms[i].elements.length;j++){
if (document.forms[i].elements[j].type=="textarea"){
alert("This is a textarea.");
}
}
}

BrightNail
Aug 14th, 2002, 06:59 PM
thanks...

I didn't see your post and came up with this on my own..I guess...need is the mother of invention...

<script language="javascript">
function testthis(){
var x = document.forms.length-1;
for (p=0;p<=x;p++){
var elemL=document.forms[p].elements.length-1;
for (y=0;y<=elemL;y++){
var formString=document.forms[p].elements[y];
if(document.forms[p].elements[0]){
if((formString.type=="textarea")&&(formString.value!="")){
alert(document.forms[p].name + ' is a textarea with something in it')
}else{ alert('not textarea or is empty')}
}
}
}

}
</script>


The only issue now is to determine if the "textbox" has focus...I am gonna tie this into a "enter key" hit ...basically, capturing a keypad hit....under 'certain' circumstances....I want to capture the "enter" key and do somethin with it IF there last
action was entering info in a textarea....

I am still working out the particulars...

thanks,
james

glenngv
Aug 15th, 2002, 04:12 AM
var focused = new Array();

function testthis(){
for (var i=0;i<document.forms.length;i++){
for (var j=0;j<document.forms[i].elements.length;j++){
el=document.forms[i].elements[j];
if (el.type=="textarea" && el.value!="" && focused[el.form.name+el.name]){
alert("This is a textarea with value and focus.");
}
}
}
}

then in each of your textareas in each form:

<textarea name="texta" onfocus="focused[this.form.name+this.name]=true" onblur="focused[this.form.name+this.name]=false"></textarea>

BrightNail
Aug 16th, 2002, 01:54 AM
Thanks,,,!!!!!!!!!!!

I will try it out...I appreciate you taking the time to respond....cool