2

I am very mediocre in Javascript and looking around I was not able to find any information on how to achieve the behaviour I want or if it's even possible.

I have a namespace form to access my HTML form:

var form = (function(){
    all   = function () $('#myform .entry');
    first = function () $('#myform .entry').first();
})();

form.all.css('color', 'blue');
form.first.css('color', 'red');

Desired extra behaviour:

form.css('background-color', 'green'); // should be calling $('#myform')

Is this possible?

1 Answer 1

2

You don't have a namespace, you have an object literal containing functions (and now you edited it, and added an IIFE, which makes even less sense), and the functions have to return something to be able to use it

var form = {
    all : function () {
        return $('#myform .entry');
    },
    first : function () {
        return $('#myform .entry').first();
    }
}

form.all().css('color', 'blue');
form.first().css('color', 'red');

or if you want to store the collection in an object without looking up in the DOM each time

var form = $('#myform');

form.all   = $('#myform .entry');
form.first = $('#myform .entry').first();

form.all.css('color', 'blue');
form.first.css('color', 'red');
Sign up to request clarification or add additional context in comments.

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.