Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"onclick" event not working in FF and Chrome

I have some SVG paths inside a div with an onclick attribute:

<path class="limbs" id="limb1" d="some coordinates here" onclick="open(1)" />

The open() function is defined in a seperate JS file, which is implemented just before the body tag (like the jQuery file as well):

function open(n) {
    $("#information").fadeIn();
    $("#info" + n).fadeIn();
}

div#info1, for example, is an information box inside div#information, a full screen semi-transparent black background (gives it a lightbox-like effect).

Everything works well using Safari. However, if I try it with FF or Chrome, the browser seems to load a new page when I click (which shouldn't happen) and it results in a blank screen with no source code.

Page can be seen here: frank.schufi.ch/3dmapping

like image 423
Frank Schaufelberger Avatar asked Mar 29 '14 12:03

Frank Schaufelberger


People also ask

Does onclick event work on mobile?

Yes it will work. This has been answered here: Would onClick event work on touch on touch-screen devices?

Can we use onclick in JavaScript?

The onclick event in JavaScript lets you as a programmer execute a function when an element is clicked.

Is onclick event handler?

The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.


2 Answers

there are some mismatch behaviors between browsers so this could be the why it happens and for the what, it seems to me other open(n) function is being called. try change the function name, to let say:

newOpen(n)

see if it helps.

like image 161
knightsb Avatar answered Oct 11 '22 03:10

knightsb


As Haocheng commented, a more up to date way of doing this is making an event handler for clicking the path. You would basically reach the same goal having this jQuery snippet instead:

$('.limbs').click(function() {
    $("#information").fadeIn();
    $("#info" + $(this).attr('id')).fadeIn();
});

I'm not sure if this will make a difference in the outcome, as someone pointed out it might have to do with the fact that you named your function open(), so it might. What I would suggest you trying if you're still having problems, is adding a preventDefault(), like this:

$('.limbs').click(function(event) {
    event.preventDefault();
    $("#information").fadeIn();
    $("#info" + $(this).attr('id')).fadeIn();
});

This removes default behaviour from HTML. For example, if .limbs would be a attached to an <a> elements, it would prevent the standard behaviour of following that link. Then again, renaming the function to newOpen() might be enough as well. :)

like image 26
Genie Avatar answered Oct 11 '22 03:10

Genie