0

html base

<html>
  <head>
  </head>
  <body>
    <input type="text" class="abc"/>
  </body>
</html>

So I have my prototype object

function AnArray(){
    this.anArray=[];
}

AnArray.prototype.getAnArray=function(val){
    return this.anArray[val];
}

AnArray.prototype.setData=function(index,val){
    this.anArray[index].data=val;
}

var objAnArray=new AnArray();

the object ends up looking like this

id: 1, pid: "questions-worth-asking", num: 1, data: null

and I'm trying to change an attribute in it like so

objAnArray.setData(0,$(".abc").eq(0).val());

When I've rune console.log messages using getAnArray() before and after the above line, it returns the same value as it has not been changed.

My question is how do you change attributes of a prototype object?

edit: This link led me down the right path http://www.gpickin.com/index.cfm/blog/websql-when-is-a-javascript-object-not-a-javascript-object

2 Answers 2

1

You're missing a lot of information from your post that makes it difficult to debug.

From my understanding the problem is that:

  1. You want your jQuery object's value property to be stored in an array that you wrapped in an object.
  2. You want to store this property with the setData function you wrote.
  3. 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:

  1. 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.
  2. 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.
  3. 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:

  1. Write all your experimental code in an isolated environment so that all the confidential content content doesn't have to be removed before posting.
  2. Run your code and make sure it runs as expected.
  3. 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.
  4. 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]);
};
Sign up to request clarification or add additional context in comments.

1 Comment

did the log inside the setData and the val returns whatever I've typed in, and the .data is null on both sides. 1) It is set 2) It's an event 3) The click event is in $(document).ready which calls the function declared outside of the $(document).ready function
1

It seems to me that the problem isn't in your javascript. You're saying that you ran a console.log before and after your setData call on objAnArray. Perhaps it has something to do with your HTML input element not being updated, or the data not coming through in time.

Like Keane said, we need more info about your set up and logic flow.

1 Comment

Try and elaborate more about the context when you call: objAnArray.setData(0,$(".abc").eq(0).val());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.