1

Please see this fiddle:

http://jsfiddle.net/GSHsH/9/

HTML:

<div id="papa" onclick="anything(this);">Blabla</div>​

JS:

function anything(theObj){
    window.alert(theObj.innerHTML);
}

I do not understand why the function "anything" gets as not reconized. (using prototype)

1
  • check the <script> tag if your js file is declared on the page header. if you're using jquery, try window.alert($(theObj).innerHTML); Commented Nov 21, 2012 at 9:30

2 Answers 2

6

It is not that it doesnt recognide this - it does not recognise the method anything because of a setting you've made in jsfiddle - to scope the javascript into onLoad. If you would have chosen no wrap (head) it would work fine: http://jsfiddle.net/GSHsH/11/

A bit more detail. The way you set it up, this is what gets injected into the output frame in jsfiddle:

Event.observe(window, "load", function(){

function anything(theObj){
    window.alert(theObj.innerHTML);
}

});

Note that the method anything is not in global (window) scope, it is in the scope of a particular function. This means its not visible to the element on the page.

The way I set it up you get this:

function anything(theObj){
    window.alert(theObj.innerHTML);
}

Which is just a plain old function defined in the head of the page - now accesible from an element on the page.

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

2 Comments

Thanks, this helped me to fix a bigger problem in one of my projects. I called a function using <a href="javascript:thefunction(this)"> and I wasn't able to use the <a> object. Instead the window object was passed.
Glad to have helped @Octavian
1

Its because jsfiddle generates your code like this:

Event.observe(window, "load", function(){
    function anything (theObj){
        window.alert(theObj.innerHTML);
    }
});

so your "anything" function is not in global scope. this will work:

window.anything = function (theObj){
    window.alert(theObj.innerHTML);
}

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.