Steven Johnston
Steven Johnston

Reputation: 1849

How to pass value to a method in a method in javascript

I am trying to learn how jquery can select an element pick an attribute and set it to a value. I have figured out how to select the element and pick an attribute. I just cant seem to set the attribute via a parameter.

This is the format that jquery uses. As i am trying to replicate

onclick='$(this).innerHTML("My new Text")'

I am trying to change the innerHTML of a button when i click it to "My new Text" or any other value.

I have came up with this from what i understand.

var $ = function(obj)
{
    var innerHTML = function(obj)
    {
        obj.innerHTML = **NEED HELP HERE**;
    }(obj);
};

How can i read the argument (in this case "My new Text") from the innerHTML() call?

Thanks

Upvotes: 0

Views: 43

Answers (1)

Barmar
Barmar

Reputation: 780724

$ needs to return an object so you can access its .innerHTML property. That function takes the HTML text as an argument, it accesses the element using a closure variable from the $ function.

$ = function(element) {
    return {
        innerHTML: function(html) {
            element.innerHTML = html;
        }
    }
};

Upvotes: 2

Related Questions