0

I am trying to make for jQuery that uses functions on an object.

What I've been trying: (index.html in script tags after adding jQuery and plugin.js)

var object = new myObject("#selected-div");
object.jQueryMadeFunction();

And the plugin.js:

function myObject(param) {
    this.param = $(param);
}

$.fn.jQueryMadeFunction = function () {
    this.param.css({
        background: "red",
        height: "100px",
        width: "100px"
    });
}

I am unsure what I am doing wrong, I have tried multiple things to get this working but now I am completely stuck. What's going wrong and how exactly do I fix it?

1 Answer 1

1

I think it should be like this:

function myObject(param) {
    this.param = $(param);
}

$.fn.jQueryMadeFunction = function () {
    this.css({
        background: "red",
        height: "100px",
        width: "100px"
    });
}

var object = new myObject("#selected-div");
object.param.jQueryMadeFunction();

and here the fiddle: http://jsfiddle.net/bgapon79/

you could return the object like so:

function myObject(param) {
    return $(param);
}

object.jQueryMadeFunction();

is it that what you mean?

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

2 Comments

Problem doing it this way is that I have to call the function on the objects parameter and not the object itself, the object itself merely selects a div for now, I want future functions to find the parameter itself it needs depending on what function you're calling. No way of doing this?
The return did exactly what I needed, I can't believe I didn't try this out of everything. Thanks!

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.