Matej Posted October 28, 2014 Share Posted October 28, 2014 Hi , im building table spoiler here http://jsfiddle.net/mLjo49rk/2/ (basicly , every text get shortened to length 1 and on click it should display full text) but i have problem with function longIt(); it basicly writes Cannot read property 'length' of undefined while i was working with array this.after in function shortIt, any1 knows how to fix it? Link to comment Share on other sites More sharing options...
Ingolme Posted October 28, 2014 Share Posted October 28, 2014 There might be confusion as to what "this" is referring to because it's inside a function that could be called from any context. Here's the solution: Make an internal variable that is explicitly set to "this" when the object is created and use that variable to refer to the object. You also forgot to define the variable "event", it should be the first parameter of the click event handler. function matej(element) var self = this; // Reference to the object // ... self.longIt = function() { for (var i = 0; i < self.children.length; i++) { self.children[i].onclick = function (e) { // Added event object. Called it "e" so it won't override global variable "event" that IE needs e = e ? e : window.event; // Compatibility for Internet Explorer var x=self.after.indexOf(e.target.innerHTML); alert("!as"); } } } // ...} Link to comment Share on other sites More sharing options...
Matej Posted October 28, 2014 Author Share Posted October 28, 2014 (edited) I added varriable which refer to this , but it has broken the code http://jsfiddle.net/mLjo49rk/3/ // i could call self. from inside the Matej() but not inside loops and prototype Edited October 28, 2014 by Matej Link to comment Share on other sites More sharing options...
Ingolme Posted October 28, 2014 Share Posted October 28, 2014 It should be safe to use the "this" keyword when you're adding a function to the prototype, it's only in the class definition where you need to have a clear reference to the object. After correcting that, I still get an error because you haven't defined "event" yet. Link to comment Share on other sites More sharing options...
Matej Posted October 28, 2014 Author Share Posted October 28, 2014 got it , thanks =http://jsfiddle.net/mLjo49rk/5/ , also , the event thing function(e) is only for IE right?(IE needs to pass it or smt) so why didnt it work in chrome ? i have never had problem with it before. Link to comment Share on other sites More sharing options...
Ingolme Posted October 29, 2014 Share Posted October 29, 2014 A global variable called "event" only exists in IE. Every other browser expects a function parameter: function(eventObject) { Link to comment Share on other sites More sharing options...
Matej Posted October 29, 2014 Author Share Posted October 29, 2014 Thanks , i thought i was vice verse Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now