Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't figure out what I'm doing wrong!! Very simple.

    Alright, I've oversimplified my code here to nail down what I'm doing wrong... Basically I'm having problems having a simple member function understand an object instance variable... Keep getting "undefined" in the alert. Pleas help!

    Code:
     var testdef;
     
    testdef = (function() {
    	
    	function testdef(davariable) {
    		this.davariable = davariable;
    		$('#dadiv').click( function() {
    			alert(this.davariable);		
    		}
    		); 
    	}
    	
    	return testdef;
    
    })();
    And I initiate the object with this:
    Code:
        $(document).ready(function() {
            var testobj = new testdef('hiya');
        });
    But the alert I get when I click my link always says undefined. I swear I have other code that looks just like this which works. Argh! Thanks for any insight you all have, I'm sure it must be some stupid typo.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <!doctype html>
    <head><title></title>
    </head><body>
    <div id="dadiv">xx</div>
     <script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
     function testdef(davariable){	
    	$('#dadiv').click( function cliked(){
    		alert(davariable);		
    	});		 
     }		
    testdef("hiyas");
    </script>
      </body></html>
    Last edited by DaveyErwin; 06-27-2012 at 08:36 PM.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok that works, how about this?

    Code:
    <!doctype html>
    <head><title></title>
    </head><body>
    <div id="dadiv">xx</div>
    <script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
    testdef = (function() {
    	
    	function testdef(davariable)
    	{
    		this.davariable = davariable;
    		$('#dadiv').click(this.deleteit);		
    	}
    
    	testdef.prototype.deleteit = function() {
    		alert(this.davariable);
    	}; 
    	
    	
    	return testdef;
    
    })();
    
    daobj = new testdef('yoyo');
    </script>
      </body></html>
    I need this to work in an object ultimately... The "deleteit" function will call a jquery get and will provide a callback function that's part of the object... so that it can keep track of the same "davariable" function, and it needs to be an object because there will be many of these running per page... So why isn't THIS code sample working?

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <!doctype html>
    <head><title></title>
    </head><body>
    <div id="dadiv">xx</div>
    <script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
    <!doctype html>
    <head><title></title>
    </head><body>
    <div id="dadiv">xx</div>
    <script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
    	
    	function Testdef(davariable)	{
    		function deleteit() {
    			alert(davariable);
    		}; 	
    		$('#dadiv').click(deleteit);
    	}	
    daobj = new Testdef('yoyo');
    daobj1= new Testdef('yoyo1');
    </script>
      </body></html>
    Or maybe ....

    Code:
    <!doctype html>
    <head><title></title>
    </head><body>
    <div id="dadiv">xx</div>
    <script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
    <!doctype html>
    <head><title></title>
    </head><body>
    <div id="dadiv">xx</div>
    <script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>	
    	function Testdef(davariable){$('#dadiv').click(davariable());}	
    daobj = new Testdef(function(){alert("yoyo")});
    daobj1= new Testdef(function(){alert("yoyo1")});
    </script>
      </body></html>
    Last edited by DaveyErwin; 06-27-2012 at 08:05 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •