0

I'm trying to understand how to call an object's method from HTML without declaring this object a global variable. Is it even possible? I don't really want to pollute the global scope, so I assume there must be a way to avoid this...

Let's say I have a javascript object which one of it's methods is called clearSelection(). This clearSelection() method just erases an html input text value and does some other stuff which isn't relevant to this question at all.

The issue is that when I declare this object inside an $(function(){}); as a local variable to that function, I can't access it from the input's onclick html attribute, as it's not defined.

My solution for this would be to declare this object as a global variable, maybe not right inside the window object but something less harmful like inside window.document.body (I believe it is worse to let it be declared right inside the window object, so I guess this is still bad...)

Here's some code for you:

My object:

function MyObject(foo,bar){
    ...
    ...
    this.clearSelection() = function(){
        // do some useful stuff here
        ...
    }
}

Script tags inside my html:

<script>
$(function(){
    var myObject = new MyObject(baz,bal);
});
</script>

My button declatarion @ html:

<button type="button" onclick="myObject.clearSelection()">Clear</button>

So, javascript console complains because myObject is not defined, and I assume it's due to the scope, because from the HTML I can't access the same scope that the object (myObject) lives in.


Current approach

This is how I'm doing it so it works, but I don't really like it and I'm asking for another cleaner way. The object structure and html button are the same so I'll skip those:

<script>
$(function(){
    window.document.body.myObject = new MyObject(baz,bal);
});
</script>
2
  • You could simplify with window.myObject = ..., but best to move the event handling to your script, as @Satpal posted. Commented Dec 12, 2016 at 11:59
  • 1
    ECMAScript6 Classes Commented Dec 12, 2016 at 12:01

1 Answer 1

4

You should use .on() and bind event handler using jQuery and get rid of ugly inline click handler.

Add ID property to button element and use ID Selector (“#id”)

$(function(){
    var myObject = new MyObject(baz,bal);
    $('#clearSelection').on('click', function(){
        myObject.clearSelection();
    })
});

HTML

<button type="button" id="clearSelection">Clear</button>
Sign up to request clarification or add additional context in comments.

6 Comments

okay, I assume that works, and surely would do it like that if I can't find any other solution, but I'd like to understand why I can't access the object from the onclick attribute... maybe somebody can point to another solution keeping the inline onclick
@JorgeGRC, You are defining myObject in document-ready scope. Which is not available globally thus you can't access the object from the onclick attribute. You can try var myObject; $(function(){ myObject = new MyObject(baz,bal); });
@JorgeGRC Inline events have been considered bad practise for a very, very long time now. This approach is far more acceptable and should be considered the right way to resolve your problem, amongst other potential issues. See here for some info... robertnyman.com/2008/11/20/…
@Archer thanks for pointing it out, I didn't know they were catalogued as bad practices, from now on I'll stop using them. I'm working on a legacy project and it's got this inline handlers everywhere so I just kept doing it without knowing it's a bad practice
@JorgeGRC I've been guilty of carrying on previously coded bad practises more often than I care to remember. At least you have the luxury to change it :)
|

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.