1

i'm trying to create my own tool in javascript, which will help me load some php functions/files into my div.

Here is my javascript:

$(document).ready(function () {
    $('.btn').click(function() {        
        $('#someid').testit();
        //   $('#someid').load('somefile');  it works
        return false;
    })   
});

function testit() { 
    $(this).load('somefile');
}

And my html:

<div id="someid"></div>
<form>
    <input type="submit" value="test" class="btn" />
</form>

I can't load the resource from external function - why is that?

5
  • 1
    It really does'nt work that way! you can't just create a regular function and chain it to a jQuery object, but you could use jQuery's $.fn constructor to make a chainable plugin, and it's all explained on the plugin authoring site of jQuery. Commented Dec 3, 2012 at 1:11
  • Thanks, but is it a way to fire my own JS function on some Jquery selector? Commented Dec 3, 2012 at 1:13
  • 3
    And again, it does'nt work that way? You could pass the element as a paremeter in the function, like : testit(filename, element), but you can't chain the function to the element. Commented Dec 3, 2012 at 1:15
  • 1
    Here's a really quick example : FIDDLE Commented Dec 3, 2012 at 1:20
  • Thanky you ; ) I'll try to do it that way - it's WAY more elegant : ) Commented Dec 3, 2012 at 1:22

4 Answers 4

2

If you want to call something like:

$('#someid').testit();

Then, you have to create a jQuery plugin that defines the method testit. That code would look something like this:

jQuery.fn.testit = function() {
    return this.load('somefile');
}

Then, and only then can you use testit() as a jQuery method like this:

$('#someid').testit();

And, it will operate on the desired selector.

Here's some of the jQuery doc on writing plugin methods like this.

Sign up to request clarification or add additional context in comments.

1 Comment

I simplified the plugin function a bit since .load() already iterates the jQuery object for you.
2

You need to do like below:

$.fn.testit = function () {
  return this.load('somefile');
}

Comments

1

Unfortunately you can't chain functions like that out of the box, you'd have to put it into the jquery.fn prototype to make a plugin.

What you could do instead is use the .each() function on the selector to call your function.

In your case, if you were to call $("#someid").each(testit); jquery will then call the testit() function on each of the elements found.

Comments

1

You can pass the element like this ...

$(document).ready(function () {
    $('.btn').click(function() {        
        testit($('#someid'));
        return false;
    })   
});

function testit(el) { 
    el.load('somefile');
}

Or create a JQuery plugin as jfriend00 mentions

Comments

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.