Calling function with onclick

Asked

Viewed 11,882 times

2

Guys I can’t call a function using onclick, calling it straight from the html tag comes normal but if I call unobstructive way without chance I’ve already read several Docs tried n ways and nothing someone can give a light.

document.getElementById("btSoyouzm").onclick=function(){buscaImagem()};

function buscaImagem(){
        alert("teste do onclick")
}
  • Tried to window.onload?

  • It wouldn’t be that: <input type="button" value="Mybutton" onclick="searchImage" /> ?

  • 1

    Works well for me... http://jsfiddle.net/yhfvY/ Can re-create problem in jsFiddle?

  • 1

    @Matheusbessa this works, but did not want to mark in html.

  • @Sergio works =/, now because the page does not work I’ve taken and put onload a thousand times.

  • @Guilhermenascimento has tried with and without window.onload

  • @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 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, 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 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.

  • Give an example of the problem using jsfiddle

Show 6 more comments

2 answers

4

So try, dear friend

document.getElementById("btSoyouzm").addEventListener("click", buscaImagem, false); 

function buscaImagem(){
        alert("teste do onclick")
}

Documentation : addeventlistener

  • Why the downvoter? What’s wrong with the answer?

  • That’s right @Silvio Andorinha’s reply!

  • @Silvioandorinha got the following browser return "add Event not is defined";

  • @Heltonss which browser you are using?

  • @Heltonss suggest a cross-browser implementation as in my answer :D

  • @I always use firefox and Chrome.

  • It was supposed to work because the addEventListener works on Chrome and filefox

Show 2 more comments

2

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);
  • guy I had already tried his answer and also did not give result the only difference is that it did not give error in the console.

  • Here is the test http://jsfiddle.net/vTv8T/

  • Guys I’m going to test on another machine has no place what is happening, I promise to give feedback later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.