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
Yes it will work. This has been answered here: Would onClick event work on touch on touch-screen devices?
The onclick event in JavaScript lets you as a programmer execute a function when an element is clicked.
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.
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.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With