Forum Moderators: open

Message Too Old, No Replies

referencing the form array element index

         

Avenlanzer

2:28 am on Sep 20, 2009 (gmt 0)

10+ Year Member


I have a form with:
(simplified for this post, there is much more data in between each)
=============

<input type='checkbox' name='thechk' onClick='document.form.advtxt[0].disabled=false;'><input type='text' name='thetxt' disabled=true>

<input type='checkbox' name='thechk' onClick='document.form.advtxt[1].disabled=false;'><input type='text' name='thetxt' disabled=true>

<input type='checkbox' name='thechk' onClick='document.form.advtxt[2].disabled=false;'><input type='text' name='thetxt' disabled=true>

=============

I need the element referentials to be the same. ie: onClick='document.form.thetxt[this.index].disabled=false;'
(or however I can manage to get that to work.)

I need to reference which element of the thechk I'm on and tell the same element of the thetxt to enable, rather than going through the literal 200 of them I have and referencing each individually. It will be a major headache when I have to change the third of 200 and everything after that to it's next element up.

I even have some versions that have javascript functions to do the same thing, but the issue seems to be having it figure out which of the thechk index elements it is looking at.

I've tried doing arrays and javascript writeLn to make this happen, but there is long paragraphs of data in between and it just isn't feasible to have them in arrays, so I need to figure out how to make this work, but no combination I've found or tried so far has made it happen and it just seems like something that shouldn't be so difficult to figure out. I'm stumped.

astupidname

11:37 am on Sep 20, 2009 (gmt 0)

10+ Year Member



Something like this what you are after?:

<script type="text/javascript">
window.onload = function () {
document.forms["someForm"].reset();
var i = 0,group = document.forms.someForm.thechk;
while(group[i]) {
group[i].onclick = function (I) {
return function () {
var el = this.form.thetxt[I];
if (el) {
el.disabled = !this.checked;
}
};
}(i);
i++;
}
};
</script>

<form action="" name="someForm">
<p>
<input type="checkbox" name="thechk"><input type="text" name="thetxt" disabled="disabled"><br>
<input type="checkbox" name="thechk"><input type="text" name="thetxt" disabled="disabled"><br>
<input type="checkbox" name="thechk"><input type="text" name="thetxt" disabled="disabled">
</p>
</form>

daveVk

12:33 pm on Sep 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Consider wrapping the related inputs in say a span

eg
<span><input type='checkbox' name='thechk' onClick='document.form.advtxt[1].disabled=false;'><input type='text' name='thetxt' disabled=true></span>

then the pair of inputs can be had by

var els = this.parentNode.getElementsByTagName('input');

els[0] is the checkbox and els[1] the corresponding text box

Avenlanzer

1:03 am on Sep 21, 2009 (gmt 0)

10+ Year Member


oh crap... I miswrote the example. here's what I meant:

<input type='checkbox' name='thechk' onClick='document.form.thetxt[0].disabled=false;'><input type='text' name='thetxt' disabled=true>

I tried to make it generic and missed something. oops. that should make more sense now.

rainborick

3:24 am on Sep 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It may just be another aspect of your attempt at making the example generic, but you need to make the value for the name attributes on all of the <input> tags unique or you'll have problems getting JavaScript or the browser to recognize all of them individually. If the tags are generated dynamically, you could just add a counter in the loop that generates them, along the lines of:

counter = 0;
while(someCondition) {
document.writeln("<input type='checkbox' name='thechk" + counter + "' onClick='document.form.thetxt" + counter + ".disabled=false;'><input type='text' name='thetxt" + counter + "' disabled='true'>");
counter++;
}

Avenlanzer

1:24 am on Sep 24, 2009 (gmt 0)

10+ Year Member



No. that's not the case that I've found, and that's part of what I'm trying to put forth here. If you include them all as the same name, it will treat them as an array, and therefore you can find which one you are talking about by referencing the element number of it. I KNOW this aspect works, it's what I use now, but what I need is for it to figure out what element index it is on when I click thechk and enable the corrisponding thetxt. I could pull the old condition writeln trick, but there is a ton of information in between each one that won't form to arrays because of it's size, and therefore counting one up on each just isn't feasible.

daveVk

3:24 am on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want the checkbox to enable/disable the following text box. So why not write "find following" script, rather than numbering or counting names ?