Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  147] [ 5]  / answers: 1 / hits: 17409  / 13 Years ago, wed, july 13, 2011, 12:00:00

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);
}

More From » html

 Answers
21

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.


[#91202] Tuesday, July 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dustin

Total Points: 599
Total Questions: 105
Total Answers: 106

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
dustin questions
;