SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Location
    Canada, Alberta
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    question about javascript object in jquery

    A quick question about objects in jquery.

    I'm having trouble calling functions in jquery events, here's how I'm doing it.

    Code:
    var myObj = {}
    
    myObj.test1 = function(str){
         console.log(str);
    }
    
    $('.selector').click(myObj.test('test')); //prints test even though nothing has been clicked
    
    //same with
    $('.selector').blind({
    	click	:	myObj.test('test')
    })
    the function get's called fine without any parameters.

    something like this will work but $(this) seems to act funny
    Code:
    $(".selector").bind({
    	click	:	function(){
    		myObj.test('test');
    	}
    }); //prints test onclick
    
    //if I run test2 the sameway as presented above, this just retruns the myObj Information
    myObj.test2 = function(a){
           console.log($(this));
    }

    Whats the best way to approach $(this)?

    How can I call a function directly ex: $('.selector').click(myFunc); And pass in parameters at the same time?

    I've just started getting into javascript so this is purely an academic endeavor

    thanks

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Objects share this in the local scope so the examples you have given won't work properly because myObj has an instance of this which allows control over the object but not the onclick event. What you need to do is the following so this is set within the correct scope but can be used in the local object scope. See the example below...

    Code JavaScript:
    var myObj = {}
     
    myObj.test = function(self) {
        console.log(self); // Local scope var set onclick
        console.log(this); // Local scope var set in this object
    }
     
    $('.selector').bind('click', function() {
        myObj.test(this);
    });
    As far as your second question goes I'm not really sure about as i have only ever dealt with passing a function reference and nothing more.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by charlestonolaes View Post
    $('.selector').click(myObj.test('test')); //prints test even though nothing has been clicked
    The problem there is that you are executing the function, which means that the function is run straight away, and the returned result from the function is assigned to the click event.

    Instead, you should put it inside of an anonymous function, so that the function can then be run later on:

    Code javascript:
    $('.selector').click(function () {
        myObj.test('test')
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Location
    Canada, Alberta
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The problem there is that you are executing the function, which means that the function is run straight away, and the returned result from the function is assigned to the click event.

    Instead, you should put it inside of an anonymous function, so that the function can then be run later on:

    Code javascript:
    $('.selector').click(function () {
        myObj.test('test')
    });
    oh cool, Thanks for the quick reponse, I was kinda hoping that it would only execute on the click event.

    will myObj.test() use parameters passed in by jquery?

    ex:

    Code javascript:
    myObj.test = function(i){
     
    console.log(i);
     
    }
     
    $('.selectors').each(myObj.test); //will this display the index(i)?

    @SgtLegend: Cool thanks, I'm glad I got that sorted out, javascript has always been a brain teaser for me.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by charlestonolaes View Post
    will myObj.test() use parameters passed in by jquery?
    Yes?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

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