onclick() not working in safari
Solution 1
As gilly3 noted, the issue you're having is that you cannot call click()
on an <img>
. This does not mean that the onclick
handler does not work. If you take your mouse and click on the image, the handler will still fire.
Now, if you want to simulate the click, this answer will give you everything you need to do that.
Solution 2
In Safari, the click()
method (which simulates a mouse click) cannot be applied to an <img>
. But, it can be applied to an <input type="image">
. Are you able to use that?
user127091
Updated on June 04, 2022Comments
-
user127091 almost 2 years
The below code works fine on IE7 but not in Safari(5.0.5). If possible, I want to avoid using jQuery. The goal is for this functionality to work on iPad but right now testing with desktop safari. Please let me know if you have any ideas on getting it to work both on IE and Safari.
<div id="test" ></div> <script> function attachCallback(node) { node.onclick = function() { alert("coming here"); } ; } var retrybutton = document.createElement("img"); retrybutton.src = "test.png"; retrybutton.alt = "retry"; retrybutton.setAttribute("id","retrybutton"); attachCallback( retrybutton ) ; var a = document.getElementById("test"); a.appendChild(retrybutton); // testing without using retrybutton var test = document.getElementById("retrybutton"); test.click(); </script> </body></html>
Update: Debating whether to go with "onmouseup" or something like below [Thanks Andres!! I'm not able to add comments]
if (Prototype.Browser.IE) { document.getElementById("retrybutton").click(); } else { // from question link in comment var event = document.createEvent("HTMLEvents"); event.initEvent("click", true, true); document.getElementById('retrybutton').dispatchEvent(event); }
-
user1844933 almost 9 yearsthanks, your answer indirectly helped me, I can understand from your answer that legacy browsers wont support click() event, Just I replaced with onclick(), now its working in both legacy and modern browsers, thanks again
-
Poonam almost 8 yearsI'm using this
<span class="fa-calendar-o"> </span>
... and a calendar icon shows. Isclick()
will not work on this in safari?? -
Poonam almost 8 yearsI'm using this <span class="fa-calendar-o"> </span>... and a calendar icon shows. Is click() will not work on this in safari??