0xhughes
0xhughes

Reputation: 2753

this.name checkbox onClick returning undefined and nothing?

sorry if this seems really basic :( I have a project I am working on and it's coming along bit by bit. I am fairly new to javascript so please excuse my noobness. I have search around here and google and have not found much in regard to my issue, I must just not be wording my searches right. Anyways. My project generates a table with 1000 thumbnails in it, each thumbnail image has a checkbox by it with a unique value and name. These 1000 thumbnails exist on an html page called by an iframe. My project could generate upwards 100,000 thumbnail images which I cycle through using the iframe. Anyways! I would like the user to be able to save their checkbox selections between iframes. I am just starting to code this portion. I figured I could pass the checkbox selection to the parent document in an array, which seems to work! I have a function which is called when a user checks a checkbox which gets the value, name and iframe page, it then concatenates that information and pushes it to the array. Here is the function.

parent.genL = new Array();
 function repGenChk() {
 var chkN = this.name;
 var chkV = this.value;
 var chkP = parent.document.getElementById("selOpt").selectedIndex;
 var chkArr = chkN+":"+chkV+":"+chkP;
 parent.genL.push(chkArr);
 alert(parent.genL[parent.genL]);
}

The issue I am having is when it alerts, all of the array items are as such, ":undefined:X" X being the page number. It should look something like such for each item pushed to the array, "3041:3041:3,1002:1002:1,10294:10294:10..." so on and so forth. The only thing it is getting is the iframe page id (the selOpt variable called in the chkP variable.). I am assuming I am handling "this" wrong, but i'm not sure how I am handling it wrong? An example checkbox looks like such...

<input type="checkbox" onclick="repGenChk();" value="9059" name="9059">

So how I would like it to work is, User selects thumbnail of interest by clicking the checkbox, the checkbox function executes pushing the "x:x:x" item to the array, later one after many checks between iframe pages some other stuff gets done with that information.

Any and all infromation, tips, thoughts and constructive criticism is very welcome! Thanks so much for your help StackOverflow community!

:)

Upvotes: 0

Views: 2405

Answers (1)

fbynite
fbynite

Reputation: 2661

I think you need to pass 'this' to repGenChk(). As it is, you aren't passing anything to repGenChk() and therefore this.name and this.value is undefined within the repGenChk function.

For the input tag:

<input type="checkbox" onclick="repGenChk(this);" value="9059" name="9059">

For the repGenChk function:

parent.genL = new Array();
function repGenChk(obj) {
    var chkN = obj.name;
    var chkV = obj.value;
    var chkP = parent.document.getElementById("selOpt").selectedIndex;
    var chkArr = chkN+":"+chkV+":"+chkP;
    parent.genL.push(chkArr);
    alert(parent.genL[parent.genL]);
}

Upvotes: 1

Related Questions