To dynamically implement an Onclick event you need to add this event to the DOM object with the addEventListener
as in the @Silvioandorinha response, however this implementation is different in other browsers in this case you can use a function to do this cross-browser implementation.
Its Function
function buscaImagem(){
alert("teste do onclick")
}
Javascript addEvent cross-browser
var addEvent = function(elem, type, eventHandle) {
if (elem == null || typeof(elem) == 'undefined') return;
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
} else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, eventHandle );
} else {
elem["on"+type]=eventHandle;
}
};
addEvent(document.getElementById("btSoyouzm"), "click", function(){
buscaImagem();
});
or
addEvent(document.getElementById("btSoyouzm"), "click",buscaImagem);
Tried to
window.onload
?– Guilherme Nascimento
It wouldn’t be that: <input type="button" value="Mybutton" onclick="searchImage" /> ?
– Matheus Bessa
Works well for me... http://jsfiddle.net/yhfvY/ Can re-create problem in jsFiddle?
– Sergio
@Matheusbessa this works, but did not want to mark in html.
– HeltonSS
@Sergio works =/, now because the page does not work I’ve taken and put onload a thousand times.
– HeltonSS
@Guilhermenascimento has tried with and without window.onload
– HeltonSS
@Heltonss, place the code at the bottom of the page. Before the
</body>
. If it doesn’t work you need to put more code here or follow one of the answers below.– Sergio
@Sergio put the code at the bottom of the page as said and all answers worked... I did not understand well what happened but I will try to study this error, if someone has a good explanation, usually put at the bottom of the page when I was to upload the file.
– HeltonSS
@Heltonss, if it worked at the bottom of the page then it is because it lacks
window.onload = function(){ /* o seu codigo aqui */};
You can test with this?– Sergio
@Sergio as incredible as it seems the code all the time was with window.onload I will search if anyone has had such error. grateful for the attention of all.
– HeltonSS
Give an example of the problem using jsfiddle
– Guilherme Nascimento