Fang helped me out a lot by showing me a simple slick fill down script but I'm having trouble implementing it in to the MooGrid I'm using to display the data.
The error message I get is "aObj[down[col]] is undefined" and I believe the error comes from the script not finding the correct table and column to work with. I'm new to DOM tree navigation and wondered if someone could give me a hand.
Looking over the code further I find that the dynamic table isn't a table, but a series of DIVs to display the data in a grid (that's how they get the fixed columns/rows and the double scrolling going so well).
So now my question is how to fill down the value of the last inputted textbox in a column of divs? My example has dynamic number of divs to make the columns/rows.
/* Fill Down */
window.onload=function() {
var aObj = document.getElementsByTagName('input');
var i = aObj.length;
while(i--) {
if(aObj[i].type=='text') {
aObj[i].onchange = function() {lastChoice(this.name);};
}
else {
aObj[i].onclick = function() {fillDown(this);};
}
}
};
function lastChoice(ref) {
var needle = /grade(\d+)_(\d+)$/i;
ref.match(needle);
down[(RegExp.$2)] = RegExp.$1;
}
var down = [];
function fillDown(obj) {
// button column
var aObj = document.getElementsByName('copy');
var len = aObj.length;
for(var i=0; i<len; i++) {
if(aObj[i]==obj) {
var col = i;
break;
}
}
// fill
var aObj = document.getElementsByTagName('input');
var len = aObj.length;
var idx = down[col];
var lastName = 'grade'+idx+'_'+col;
var val = document.getElementsByName(lastName)[0].value;
for(var i=0, count=0; i<len; i++) {
if(aObj[i].name==lastName) {
aObj[i].value = val;
lastName = 'grade'+(++idx)+'_'+col;
}
}
}
/* End Fill Down */
At least 98% of internet users' DNA is identical to that of chimpanzees
Upon putting the script on my page it worked first try in FF with no errors; however, it's not working in IE8 and I'm not getting an error code to give me a clue where to look.
I'll do a little research and see if I can figure out the problem but I'd love some ideas of where to start.
One more question Fang....if I wanted to use the id (which are the same as the name) rather than the name would I simply change:
Code:
/* Fill Down */
function initFillDown() {
var aObj = document.getElementsByTagName('input');
var i = aObj.length;
while(i--) {
if(aObj[i].type=='text') {
aObj[i].onblur = function() {lastChoice(this.id);};
}
else {
aObj[i].onclick = function() {fillDown(this);};
}
}
};
function lastChoice(ref) {
var needle = /grade(\d+)_(\d+)$/i;
ref.match(needle);
down[(RegExp.$2)] = RegExp.$1;
}
var down = [];
function fillDown(obj) {
// button column
var aObj = document.getElementsByName('copy');
var len = aObj.length;
for(var i=0; i<len; i++) {
if(aObj[i]==obj) {
var col = i;
break;
}
}
// fill
var aObj = document.getElementsByTagName('input');
var len = aObj.length;
var idx = down[col];
var lastName = 'grade'+idx+'_'+col;
var val = document.getElementById(lastName)[0].value;
for(var i=0, count=0; i<len; i++) {
if(aObj[i].id==lastName) {
aObj[i].value = val;
lastName = 'grade'+(++idx)+'_'+col;
}
}
}
/* End Fill Down */
When I tried this initially I was getting the error getElementById(lastName)[0] is undefined.
I wanted to switch to id because I already have some php written that uses a different name that I'd like to use with it. I can see if I can change the name referenced in the php to match what is used here. Thanks!
For those that may be interested, here's the code changed to handle the id attribute instead of the name attribute. It's running good now:
Code:
/* Fill Down */
function initFillDown() {
var aObj = document.getElementsByTagName('input');
var i = aObj.length;
while(i--) {
if(aObj[i].type=='text') { // onchange to onblur (some instances onchange wasn't allowing a 0 as a value)
aObj[i].onblur = function() {lastChoice(this.id);}; // changed this.name to this.id
} else {
aObj[i].onclick = function() {fillDown(this);};
}
}
};
function lastChoice(ref) {
var needle = /points(\d+)_(\d+)$/i;
ref.match(needle);
down[(RegExp.$2)] = RegExp.$1;
}
var down = [];
function fillDown(obj) {
// button column
var aObj = document.getElementsByName('copy');
var len = aObj.length;
for(var i=0; i<len; i++) {
if(aObj[i]==obj) {
var col = i;
break;
}
}
// fill
var aObj = document.getElementsByTagName('input');
var len = aObj.length;
var idx = down[col];
var lastName = 'points'+idx+'_'+col;
var val = document.getElementById(lastName).value; // changed document.getElementsByName[0] to document.getElementsById
for(var i=0, count=0; i<len; i++) {
if(aObj[i].id==lastName) { // changed aObj[i].name to aObj[i].id
aObj[i].value = val;
lastName = 'points'+(++idx)+'_'+col;
}
}
}
/* End Fill Down */
Oh, I almost forgot, a big THANK YOU for your help. I wouldn't have been able to write this script from scratch so I really appreciate you're taking the time to work with me. I'm starting to understand a little better JavaScript and jquery, but I have a super long way to go =)
Bookmarks