0

Code works across all major browsers, but firing a simple alert on click is not working.

This is in my header

<script type="text/javascript">
    function this_function() {
        alert("got here mango!");
    }
</script>

This is in the body

<button type="button" onclick="this_function()">click me</button>

If I put the "onclick" into the tag then it works fine and dandy.

Any and all suggestions on how to get this to work in IE would be great. Thanks in advance.

Sorry, by "into the tag" i meant putting onclick="alert()" into the tag.

2
  • 1
    I'm confused with the 'If I put the "onclick" into the tag then it works fine'. The source that you show us have the onclick. Are you trying with another way? Can you provide a jsfiddle with your error? Commented Jun 4, 2012 at 21:13
  • In IE8 running in IE7 mode it works. Commented Jun 4, 2012 at 21:14

2 Answers 2

1

Try: <button type="button" onclick="javascript:this_function();">click me</button>

It's advised to separate JavaScript and markup. Thus regardless you should assign an ID to the button and attach the onclick handler like this:

document.getElementById("button").onclick = function() {
     alert("got here mango!");
};
​
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. I tried what you suggested, but it did not work. :/
@Jivings: He has his script in <head>, so he will need a DOMready handler before setting onclick properties.
0

Are you running this sandboxed? If you aren't I would highly suggest trying this all by its self in a single HTML file with no other things going on. It is possible that IE7 is blowing up (quietly) on another script issue that is preventing your "this_function" from loading into the DOM properly.

After you have done this put the in your there is no need for it to be in the head and I have actually seen this cause problems under certain conditions

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.