Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind click event to object tag?

I have this code

<object data="images/logo.svg" type="image/svg+xml" class="icon-logo"></object>

and jquery

$(".icon-logo").click(function() {
.....
});

but I can't click event.

like image 289
Pirune Avatar asked Sep 18 '14 15:09

Pirune


People also ask

Can we add click event to div?

To add a click event listener on div tag using JavaScript, we can call the addEventListener method on the selected div. to add a div with a class. Then we write: const div = document.

How do you trigger an event on click?

click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

How do I trigger a click event without clicking?

If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.

How click event works in JavaScript?

An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.


2 Answers

The easiest way to accomplish this goal is to wrap the object tag in a div, bind the click listener to the div, and add pointer-events: none to the object tag's styles.

Sample fiddle:

.clickable {
  background: red;
  width: 100px;
  height: 100px;
  border-radius: 100px;
  overflow: hidden;
}

object {
  width: 100%;
  pointer-events: none;
}
<div class='clickable' onclick='alert("WOW")'>
  <object type='image/svg+xml' data='https://douglasduhaime.com/assets/images/icons/home.svg' />
</div>
like image 63
duhaime Avatar answered Sep 21 '22 19:09

duhaime


1. Issue: Event handling

Concerning the jQuery part, try to use event delegation.

From the docs:

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object.

$(document).on('click', '.icon-logo', function(event) {
    document.write('Event type: ' + event.type);
    document.write('<br>CSS-Class: ');
    document.write($(this).attr('class'));
});
// As said in the docs, you can attach multiple events to multiple selectors. 
// A typical example of use may be:
// $(document).on('change blur', '.icon-logo .some-other-class', function() {...}
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<object data="images/logo.svg" type="image/svg+xml" class="icon-logo" style="cursor: pointer;">Click me!</object>

Edit after @Kaiido's comment:

2. Issue: The <object> element can't be clicked.

One possibility could be to not use an <object> at all but an <img> tag instead as suggested in this SO answer: make an html svg object also a clickable link.


2. Issue: Empty HTML-tag

This kind of tag <object> needs some content to show up on the page.

Your tag:

<object data="images/logo.svg" type="image/svg+xml" class="icon-logo"></object>

is not having any inner HTML-Content, so you won't be able to click it.

like image 26
toesslab Avatar answered Sep 21 '22 19:09

toesslab