JavaScript can be used to deactivate the onclick attribute

Is there a way to deactivate one onclick event once the other has been activated? Both divs are initially hidden in the CSS. I'm new to programming, so any help is appreciated.

Markup

<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>

Script

function showDiv(id)
{   
    if(id == "leftbutton"){
        document.getElementById('orangediv').style.display = 'block';
    }else{
        document.getElementById('greendiv').style.display = 'block';
    }
}

Answer №1

Here is the solution you are looking for:

function displayDiv(id)
{   
  if(id == "button1"){
     document.getElementById('blueBox').style.display = 'block';
     document.getElementById('button2').onclick = null;
     }else{
     document.getElementById('redBox').style.display = 'block';
     document.getElementById('button1').onclick = null;
}}

Answer №2

Here is a different variation:

const toggleDiv = (id) => {   
  if (id === "btn1") {
    document.getElementById('div1').style.display = 'block';
    document.getElementById('btn2').onclick = "#";
  } else {
    document.getElementById('div2').style.display = 'block';
    document.getElementById('btn1').onclick = "#";
  }
}

Answer №3

Here is a sample script that demonstrates similar functionality:

function displayElement(elementId) {   
    var red = document.getElementById('reddiv');
    var blue = document.getElementById('bluediv');

    if (elementId == 'rightButton') {
        red.style.display = 'block';
    } else {
        blue.style.display = 'block';
    }

    red.removeAttribute('onclick');
    blue.removeAttribute('onclick');
}

Answer №4

To disable the onclick function of another element, simply set it to null.

if  (id == "btn1"){
        document.getElementById('div1').style.display = 'block';
        document.getElementById('btn2').onclick = null;
    }
    else{
        document.getElementById('div2').style.display = 'block';
        document.getElementById('btn1').onclick = null;
    }
}

Answer №5

function displaySection(id) {
    if (displaySection.running) {
        return false;
    }

    if (id === "button1") {
        document.getElementById('sectionA').style.display = 'block';
    } else {
        document.getElementById('sectionB').style.display = 'block';
    }

    displaySection.running = true;
}

displaySection.running = false;

By following this method, you can easily re-enable the display feature by setting displaySection.running back to false.

Answer №6

To determine if the adjacent element is visible, a simple test can be conducted:

function toggleVisibility(id) { 
  if (id == "menuButton" && (document.getElementById('submenu').style.display == 'none')) {
      document.getElementById('popupMenu').style.display = 'block';
  } else if (id == "closeButton" && (document.getElementById('popupMenu').style.display == 'none')) {
      document.getElementById('submenu').style.display = 'block';
  }
}

Answer №7

To make a div visible after clicking, you can set the href attribute as the id of the div. The showDiv function takes the entire element as an argument, giving you access to its attributes. You can also specify the id of the element to hide as a second argument.

<a id="toggleBtn1" href="hiddenDiv1" onclick="showDiv(this,'toggleBtn2');return false">Click me</a>
<a id="toggleBtn2" href="hiddenDiv2" onclick="showDiv(this,'toggleBtn1');return false">Click me</a>

In your JavaScript:

var showDiv = function(el, toHide){
  document.getElementById(el.href).style.display = 'block';
  document.getElementById(toHide).style.display = 'none';
  el.onclick = null; // Remove the onclick declaration from this anchor.
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for retrieving modified data from a smart table in Angular 4

Currently, I am working on an angular project where I am utilizing smart table. Here is a snippet of my .html file: <ng2-smart-table [settings]="settings" [source]="source" (editConfirm)="onSaveConfirm($event)" (deleteConfirm)="onDeleteConfirm($event ...

How to organize and reuse code efficiently with Node.js, EJS, and front-end JavaScript?

It seems that I may have chosen the wrong platform by posting this question on the 'Software Engineering' exchange: Currently, my focus is on learning the MEAN stack (I have yet to dive into Angular, so for now I am using pure vanilla JS for the ...

What is the reason for the continual influx of new users being added to the database?

I have a Node.JS and MongoDB console application where I've implemented adding users in one file and outputting all collection objects to the console in another file. When running the command - node scripts/get_all_users.js, both existing users are di ...

Utilizing jQuery to Record the Status of HTML5 Forms

Can jQuery be used to track the status of an HTML5 form element? For instance, if a user enters an invalid email address in the email field, can jQuery detect this event and take action such as disabling the submit button until a valid email is entered? ...

Discover the worth within the outcome obtained from the AJAX request

I have an action that returns a tuple containing a boolean value and a string. How can I retrieve the first boolean value from the result, which could be either true or false? This is the action: public Tuple<bool, string> Check This is the AJAX c ...

Steps to fully eliminate the recent action panel from Django's administrative user interface

Is there a way to completely remove the recent action panel from Django admin UI? I have searched extensively but all the information I find is about clearing the data in the panel from the database. What I'm looking for is a way to entirely eliminate ...

Using AJAX to set a session variable in PHP

Upon clicking a div, this JavaScript function is executed: $('.test').click(function(e) { e.preventDefault(); $.ajax({ url: 'ajax.php', type: 'POST', data: {"id": "<? ...

What could be causing the excess space in the right corner of my website's mobile version?

After creating a practice website deployed on Vercel, I ensured it was responsive by incorporating breakpoints for different screen sizes. However, upon viewing the dashboard page on my mobile device, I noticed some unwanted extra space in the top right co ...

In the world of React in Meteor, the command event.preventDefault() doesn't seem

I have encountered an issue with my application development. I am utilizing a submit form in Meteor with React, and although I am using event.preventDefault(), the page continues to reload every time the submit button is clicked. Following a brief delay, i ...

Insert a numerical value into a list to make a series of numbers whole

I currently have an array of objects that looks like this: var arr = [ { "code": "10", }, { "code": "14", } ] My goal is to expand this array to contain 5 elements. The numbers should ran ...

Establish a jQuery cookie to store language preferences

On the website I oversee, I want to implement a way to set a cookie for the selected language. The only information available is that users choose their preferred language by clicking on a dropdown menu with distinct classes assigned to each language - on ...

How to properly Open a "div" Element by its ID in ReactJS

Today, I want to share an issue that I've been facing. To simplify things, I have mapped a BDD, The result of this mapping is displayed in multiple cards, There's a button that when clicked opens up more details on the map, But here's wh ...

Ways to add elements to the DOM using jQuery from the local storage?

I'm facing a challenge where I have an input field and store the inputs in local storage. When I click on 'add', I want to immediately append the new inputs to my ul element. However, simply appending the current input won't work as it ...

Running the JavaScript code on a webpage using Selenium WebDriver

I am currently in the process of creating an automated test for a website, and I am encountering some difficulty trying to execute a specific function within the site's code. Upon inspecting the developer tools, I have identified the function I need ...

Leveraging Components within Components in Vue 2

Here is the code snippet I am working with: import './menu-item'; import ItemImage from "./item-image"; Vue.component('quest-card', { props: { title: String, isFree: Boolean, points: Number, ...

Challenge implementing custom javascript to display categorical/string features on Shiny slider

I'm attempting to design a unique Shiny slider that represents the months of the year. My desired outcome is for the slider to display the names of the months as strings, rather than numeric values where 1 corresponds to January, 2 corresponds to Febr ...

Trigger an event when hovering over a disabled item in React using material-ui's tooltip feature

I've been trying to figure out how to hover over a disabled item with a tooltip, but it seems that disabled items don't trigger any events. I attempted wrapping my elements in another div as a workaround, but unfortunately, it didn't work. I ...

Hiding Modal Box Upon User Login: A Step-by-Step Guide

After a user clicks the login button in my navigation, a modal box pops up. However, once the user logs in, the modal box does not disappear. How can I hide or remove the modal box when users click on the login button? This code snippet is from Home.vue: ...

Tips for hiding a sidebar by clicking away from it in JavaScript

My angular application for small devices has a working sidebar toggling feature, but I want the sidebar to close or hide when clicking anywhere on the page (i.e body). .component.html <nav class="sidebar sidebar-offcanvas active" id="sid ...

Tips for displaying Japanese characters followed by numbers in a single line on a web browser using CSS

Combining Japanese characters with numbers without spacing can present formatting challenges. For example, with a string like "新しいフォルダ000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ...