3

I am trying to create a custom jquery function which has two parameters, the one a single string defining the method in which to update the second parameter; an object of selectors.

Here is an example of the usage of the function:

$.select_content("update", {
    "object_1",
    "object_2"
});

And the function:

(function ( $ ) {
    var methods             = {
        "update"            : function ( objects ) {
            $.each(objects, function (object){
                // Do stuff...
            });
        },
        "reset"             : function ( objects ) {
            $.each(objects, function (object){
                // Do stuff...
            });
        }
    };
    $.fn.select_content     = function ( method ) {

    };
})( jQuery );

However I get a return error in my console which exclaims a unexpected token , on the this line: "object_1",",.

Is there a better way to approach this? Thank you.

2 Answers 2

3

Because the json syntax require key value pair. You can try an array:

$.select_content("update", [
    "object_1",
    "object_2"
]);

But you can also use the selector syntax:

$.select_content("update", "object_1, object_2");

Then:

$(objects).each(function (object){
    // Do stuff...
});
Sign up to request clarification or add additional context in comments.

2 Comments

+1 as you're right about the syntax error. Note, though, that it's not "JSON syntax". It's "object literal" syntax. JSON is a data transport format involving serialized data structures using a subset of JavaScript literal syntax.
I completely forgot that objects required a pair and arrays didn't. Hehe, thanks :3
2

While I don't understand exactly what you're trying to do, your syntax error comes from this code sample:

$.select_content("update", {
    "object_1",
    "object_2"
});

In your second param, you are using object literal notation ({}, what some might mistakenly call JSON) without naming key/value pairs. That particular syntax error will go away if you use array literal notation instead:

$.select_content("update", [
    "object_1",
    "object_2"
]);

Or if you add key names:

$.select_content("update", {
    one: "object_1",
    two: "object_2"
});

Whichever of those changes you choose will fix the syntax error you're concerned about, as I said, but may not make the thing work. There are other issues in what you're showing us. A few are:

  1. You show your function being defined on the jQuery prototype ($.fn), making it an instance object. However, you are calling it as if it were "static" via $.yourFunc()
  2. You are defining your method on the jQuery prototype but you are not returning the collection against which it was run--which kills chaining.

Based on those two items, I suspect you meant to define you method under $ as opposed to $.fn. But I cannot be sure of your intent.

1 Comment

How would I go about creating a static function with the two parameters I mentioned in my original post? Using it without the $(content).myFunc() and rather $.myFunc()?

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.