You're missing a lot of information from your post that makes it difficult to debug.
From my understanding the problem is that:
- You want your jQuery object's
value property to be stored in an array that you wrapped in an object.
- You want to store this property with the
setData function you wrote.
- You want to retrieve that object by using the
getAnArray function you wrote.
I don't think this is an issue with prototypes, but with the lack of information given to us, it could be any number of things. I wouldn't be surprised if you came back and said "Oh, it was something else completely."
I've made a fiddle that successfully sets and gets data from the anArray objects that you can use as an example.
Here are some problems you want to look at that will help you debug:
- You don't set the
anArray[index] object in this snippet. So if we are to take this at face value, the setData function should throw a ReferenceError.
- You haven't told us if you're calling the
setData function inside an event or right when the page loads. If it's the latter, then according to the html you posted at the top, you won't have any data in the input field yet. You need to call the setData function only when there's data in the field.
- You might be calling the jQuery object outside of the
$(document).ready(function(){ ... }) call so the value you're obtaining with $(".abc") call is undefined.
Give those a try and hopefully those work.
To make your questions easier to debug going forward:
- Write all your experimental code in an isolated environment so that all the confidential content content doesn't have to be removed before posting.
- Run your code and make sure it runs as expected.
- Show us all of that code so that we know where all the data comes from and how each element interacts with the other elements. For example, we currently don't know how the
anArray array is populated so I've had to make assumptions. We also don't know how id, pid, or "questions-worth-asking" comes from so there might be side effects from how those are loaded in.
- Write your code using some sort of style guide to make it easier to read. This will also help improve debug time for you and will help prevent errors from silly mistakes that you might make.
Edit:
I know you're calling console.log before and after the setData method. Consider putting console.log inside the setData method as well.
AnArray.prototype.setData = function (index, val) {
console.log("Entering setData with: ", index, val, this.anArray[index]);
this.anArray[index].data = val;
console.log("Exiting setData with: ", this.anArray[index]);
};