5

I have a list item with an onclick event. It runs in Chrome and Internet Explorer, but not Firefox. Any suggestions?

<li onclick="alert('test');">test test<br></li>
5
  • 1
    Check whether JavaScript is disable in Firefox?
    – Chandana
    Jun 1, 2012 at 14:40
  • other javascript events works fine just onclick does not
    – hdayi
    Jun 1, 2012 at 19:10
  • you shouldn't be hardcoding your onclick events into your html anyways. separate your concerns and wire up your events in a script file.
    – Jason
    Jun 1, 2012 at 22:03
  • Can you please post a complete page showing the problem. Jun 2, 2012 at 0:38
  • 2
    @hdayi There can be JavaScript Error in you page. Go to the Error Console and Check whether is there any JavaScript error. Tools --> Web Developer --> Error Console
    – Chandana
    Jun 2, 2012 at 1:04

4 Answers 4

4

This works fine for me in Firefox.

Check this:

  1. JavaScript is enabled in your browser.

  2. Try adding a return false; statement in your tag.

Or a function like this:

function test() {
    //your code here
    return false;
}
  1. Or use this:

<a href="#" onclick="alert('hi');">Link</a>

or this

<a href="javascript:void(0)" onclick="alert('hi');">Link</a>

2
  • thank you so much. the last suggestion worked. But i could not understand why only onclick event doesn't work. Mousemove, out etc all works. anyway problem solved :)
    – hdayi
    Jun 1, 2012 at 19:07
  • 1
    I think Firefox needs a return. You may also use a button if you want to. Jun 1, 2012 at 20:40
0

I was trying to minimize my html code to send a complete code to simulate the error as Boris Zbarsky requested. Then I found my mistake.

I was using marquee tag which has been deprecated. Now I am going to use jQuery instead of it.

thx

1
0

Attributes can be ignored by Firefox when it is served invalid HTML, use https://validator.w3.org/ to clean up the HTML.

-1

In Firefox, the event object is not global. You have to access it within your script tags not in html.

onclick works likes this

<li id="alert">test<br></li>

<script>
  document.getElementById("alert").addEventListener("click", function( event ) {
    alert('test');
  }, false);
</script>
3
  • 1
    That's not true and it's no longer relevant to the question. Aug 2, 2017 at 19:17
  • @user1133275, how would you write an event listener? Would you write it into the html like originally proposed?
    – daniella
    Aug 2, 2017 at 23:18
  • the example in the question is valid. Which of the valid methods one chooses to use in different contexts is a personal preference and therefore not a good stackoverflow question.... it is good practice to separate languages so limiting all JavaScript to one .js file would be my recommendation even if I don't strictly adhere to it personally. Aug 3, 2017 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.