2

Can you put 2 JavaScript onClick events in a single element, this is what I have tried so far, I've tried calling functions but that wouldnt work either.

<div class="contactme"><a href="#contactnav" onclick="document.getElementById('download').style.display='none'; document.getElementByID('skype').style.display='none';">Message me!</a></div>

Calling functions:

function hide(){
document.getElementById('download').style.display='none';
document.getElementById('skype').style.display='none';
}

HTML:

<div class="contactme"><a href="#contactnav" onclick="hide();">Message me!</a></div>

However none of them seem to work.

Scimonster
  • 32,893
  • 9
  • 77
  • 89

6 Answers6

0

Of course, you can do it in pure Javascript. Your HTML doesn't need the onclick property at all.

HTML:

<div class="contactme"><a href="#contactnav">Message me!</a></div>

JavaScript:

var contact = document.getElementsByClassName("contactme")[0];

contact.addEventListener("click", function() {
    document.getElementById('download').style.display='none';
    document.getElementByID('skype').style.display='none';
}

Now you can use contact.addEventListener to add any function you want, like this:

contact.addEventListener("click", hide);
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
0

try this

http://jsfiddle.net/ErnestoOsuna/2pjfL5oq/ Message me!

element=document.getElementById('some_id');
element.addEventListener('click',some_func);
element.addEventListener('click',some_func2);
Ernesto
  • 301
  • 1
  • 6
0

Your both code is correct but, in first code you typed 'document.getElementByID'. just fixed it.

VMT
  • 191
  • 10
0

Use element.addEventListener(Event, Function, false);

document.getElementById('ELEMENTID').addEventListener('click', function(e) {
    // onclick code here
}, false);

document.getElementById('ELEMENTID').addEventListener('click', function(e) {
    // more code here
}, false);
Luc
  • 3,581
  • 3
  • 22
  • 24
-1

How to call multiple JavaScript functions in onclick event?

And about your snippet,

onclick="hide()"
Community
  • 1
  • 1
-1

You can do it logically i.e:

<div class="contactme">
<a href="#contactnav" onclick="myfun('one')">Message me!</a>'
</div>

Now in javascript do this programatically:

function myfun(var execute){

    if(execute=="one"){
    // execute this if it is one
    }
    else(execute=="two"){
    // execute this if it is two
    }

}

Now here whenever you call myfun('one') it execute your first condition and myfun('two') it execute second.