-2

I know i have an error on this and i feel so new but i need to ask for help. Here is the code i need to fix. I get the follow error: Uncaught TypeError: Cannot read property 'find' of undefined

 <div class="plugin" data-plugin="true"></div>

   <script>window.jQuery || document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"><\/script>')</script>
 <script>
         ;(function($){
     var namespace = 'plugin';

     function Plugin(element,options){
       this.settings = options;
       this.version = '0.0.1';
       this.$element = element;
       this.init();
     }

     Plugin.prototype.formatString = function(key,val){
       return this.$element.find('ul').append('<li><strong>' + key.toUpperCase() + ':</strong> ' + val + '</li>');
     };

     Plugin.prototype.printSettings = function(){
       this.$element.append('<ul>');

       $.each(this.settings, this.formatString);
     };

     Plugin.prototype.init = function(){
       this.printSettings();
     };

     $('.plugin').each(function(key,val){
       if (!$(this).data[namespace]) $(this).data[namespace] = new Plugin($(this),$(this).data());
     });
   })(window.jQuery);
     </script>

this.$element.find('ul') seems to be the code causing the error. I have tried to add empty <ul></ul> tags to the empty div but that didn't do anything. I feel the .find is not deprecated and it looks like i formatted this correctly but i bet that i am wrong.

user2025730
  • 350
  • 4
  • 19
  • 2
    Please don't dump your complete code. Narrow it down to minimum which is required to reproduce the problem. It will help everyone and also please format the code. – Satpal Apr 11 '16 at 12:12
  • Please post the full error message. – Gosha U. Apr 11 '16 at 12:13
  • 2
    Is this a homework question? That is how it sounds like it is worded. Where is the error coming from in the code? – epascarello Apr 11 '16 at 12:14
  • Yes, it's a homework question. I guess the answerer should at least try and help explain the problem so they learn something. – Chuck Le Butt Apr 11 '16 at 12:14
  • Not trying to be lazy guys and this is for a larger project for me.Sorry i thought it seemed to be a little newbe but i cant figure this out. – user2025730 Apr 11 '16 at 12:19
  • Run the code and look at the line number next to the error in your browsers console. – Turnip Apr 11 '16 at 12:20
  • Please i really want to learn this and im making it too hard. Please any answer will help with the explanation to why? Sorry again guys. – user2025730 Apr 11 '16 at 12:20
  • 1
    Uncaught TypeError: Cannot read property 'find' of undefined – user2025730 Apr 11 '16 at 12:21
  • I get that error above when i run the code. – user2025730 Apr 11 '16 at 12:21
  • 1
    Plugin.prototype.formatString = function(key,val){ return this.$element.find('
      ').append('
    • ' + key.toUpperCase() + ': ' + val + '
    • '); }; This seems to be the code causing the error. I have tried to add empty
        tags to the empty div but that wouldnt do anything. I feel the .find is not deprecated and it looks like i formatted this correctly but i bet that i am wrong. ughh
      – user2025730 Apr 11 '16 at 12:23
    • To be fair to user2925730, it's a poorly worked question. – Chuck Le Butt Apr 11 '16 at 12:23
    • 1
      This is [**off-topic**](http://stackoverflow.com/help/on-topic): "Questions asking for _homework help_ must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." – redbmk Apr 11 '16 at 12:24
    • @user2025730, You should really try to work the question out yourself instead of just posting your homework on here without showing any attempt to solve it. – redbmk Apr 11 '16 at 12:26
    • @redbmk Again, to be fair, they have tried to solve it. – Chuck Le Butt Apr 11 '16 at 12:27
    • Well this is for an intranet site and i feel i already got into trouble posting how i did. I know the "Find" is looking for a
        but there is not one in the empty div.
      – user2025730 Apr 11 '16 at 12:27
    • I would really like to learn here guys any help would be greatful. How would you like the question formatted? – user2025730 Apr 11 '16 at 12:28
    • Changing this line doesnt fix the issue: Plugin.prototype.formatString = function(key,val){ return this.$element.find('
        ').append('
      • ' + key.toUpperCase() + ': ' + val + '
      • '); };
      – user2025730 Apr 11 '16 at 12:34
    • It sounds like `this.$element` is undefined. Chrome has a great debugger. Maybe it would help to put breakpoints in where it defines what `this.$element` is in the constructor to see if its `element` is what you expect. Another good spot for a breakpoint might be on the line where it's crashing. If `element` is good in the constructor, then do you have the right `this` in the `formatString` function? – redbmk Apr 11 '16 at 12:35
    • When i do this it seems like i get "$" is not a function so i go back up to this line and see (function($){ So Should this be a defined name eg output...? – user2025730 Apr 11 '16 at 12:58
    • Possible duplicate of [Detecting an undefined object property](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Henders Apr 11 '16 at 14:06
    • I was thinking i was calling the formatString before the this.$element but i added: $.each(this.settings, this.element, this.formatString); But now i get a new error saying: Uncaught TypeError: Cannot read property 'call' of undefined – user2025730 Apr 11 '16 at 15:24
    • I tried to not load jQuery dynamically and just used a script tag that did not work. Basically, from what I can I might have a race-condition happening somewhere. Something is loading before the dom is ready. Plus, I've had issues in the past with appending an element to the dom and then trying to use jQuery.find to assign it to a variable. I might be better off to assign that element to a variable first, then append it to the dom. At that point, I won't need jQuery.find, which is just as well because dom traversal is slow to begin with. – user2025730 Apr 11 '16 at 15:44
    • The main issue here is that there is a single line that fixes the issue with this code that i am not seeing. Please help i came here to learn from mistakes not to be bashed for wanting to learn... – user2025730 Apr 11 '16 at 15:45
    • Thank you to everyone who pushed me to do this on my own. I felt dumb enough asking the question but after the time it took me and the comments on here i felt even worse. To everyone that left helpful messages i applaud you to look above and beyond and to be helpful i appreciate you! Thank you again. – user2025730 Apr 11 '16 at 19:13

    1 Answers1

    0

    // Comments - jquery doesn't need to use find in this instance. I think the div with plugin class was just sitting there on the page and a simple reference was all that was needed.

    So to be honest i could use some help onto why this worked and the correct terminology as to why so i can educate my self better and not ask such dumb questions.

    In the end i ended up removing .find since jQuery doesnt need it for this instance. element was defined so i knew something wasn't getting outputted to the div. I ended up playing around and got the errors to a point where i could investigate things further using Google Chrome.

    Now i am simply taking this element which is the div class plugin and appending the options as a list into it. this is done with the additional line return this.$element;

    Please let me know if that is a good explanation or not. If you can add to my education here i would appreciate it.

    Plugin.prototype.formatString = function (key, val) {
        this.$element = $(".plugin").append('<li><strong>' + key.toUpperCase() + ':</strong> ' + val + '</li>');
        return this.$element;
    }
    
    ankitkanojia
    • 3,072
    • 4
    • 22
    • 35
    user2025730
    • 350
    • 4
    • 19
    • So this answer was not correct so to speak. It added the correct data but the method that was needed was to use .proxy to create the Plugin: True. Can anyone direct me towards this? – user2025730 May 03 '16 at 13:52