Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling onclick attribute with javascript

How do I disable the other onclick event once one of them has been activated. Both divs are set to display:none; in the CSS. This is probably really simple but I am new to programming.

HTML

<a id="leftbutton" href="#" onclick="showDiv(this.id); return false;">click me</a>
<a id="righttbutton" href="#"onclick="showDiv(this.id); return false;">click me</a>

Javascript

function showDiv(id)
{   
  if(id == "leftbutton"){
     document.getElementById('orangediv').style.display = 'block';
     }else{
     document.getElementById('greendiv').style.display = 'block';
}}
like image 981
ocat Avatar asked Aug 14 '12 08:08

ocat


People also ask

How do you remove onclick attribute in JS?

getElementById( toHide ). style. display = 'none'; el. onclick = null; //remove onclick declaration from this anchor. }

Does disabled disable Onclick?

When you use the disabled variant on a button, the onClick will fire. If you don't add this variant, it will prevent the onClick from firing as expected.

How do I stop div clicking?

To disable clicking inside a div with CSS or JavaScript, we can set the pointer-events CSS property to none . Also, we can add a click event listener to the div and then call event. preventDefault inside.


1 Answers

this should do the trick:

function showDiv(id)
{   
  if(id == "leftbutton"){
     document.getElementById('orangediv').style.display = 'block';
     document.getElementById('righttbutton').onclick = null;
     }else{
     document.getElementById('greendiv').style.display = 'block';
     document.getElementById('leftbutton').onclick = null;
}}
like image 125
cptHammer Avatar answered Sep 20 '22 00:09

cptHammer