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

Revamp the website's design

I am looking to revamp the color theme of my website with just a click of a button. Can someone provide me with a link to a reference website where I can get some inspiration? ...

How can one easily retrieve the callback function arguments from outside the function?

Here is a snippet of my code: var jenkins = require('jenkins')('http://192.168.1.5:8080'); var job_name = undefined; jenkins.job.list(function doneGetting(err, list) { if (err) throw err; job_name = list[0].name; }); jenkins. ...

An issue of an infinite loop arises when utilizing the updated() lifecycle hook in VueJS

I am working on a VueJS application where I need to fetch a list of articles in one of my components. The logic is such that if the user is logged in, a specific request is made, and if not, another request is executed. Below is the snippet of code: <s ...

GraphQL query excluding empty fields for various types of objects

Utilizing Apollo Graphql, I attempted to employ inheritance for retrieving various types of models without success. My goal is to extract specific fields from the objects while omitting those that are unnecessary. To address the issue of incomplete object ...

Throw an error if the entry is not found in the Firebase database

I have an array containing various objects. Users should be able to access all objects using moduls/, and a specific one with moduls/$id. However, if the requested modul does not exist, the database should return an error to inform the client that there is ...

I am in need of a efficient loader for a slow-loading page on my website. Any recommendations on where to find one that works

I'm experiencing slow loading times on my website and I'm looking to implement a loader that will hide the page until all elements are fully loaded. I've tested several loaders, but they all seem to briefly display the page before the loader ...

Unable to display text overlay on image in AngularJS

I am experiencing an issue with displaying captions on image modals. .controller('HomeController',['dataProvider','$scope','$location', '$modal', '$log', 'authService', function ...

Create a custom div class specifically designed for an image button

Apologies if my title is a bit misleading, I struggled to find the right phrasing. Currently, I have images linked to various social networks using href. For instance, clicking on the facebook icon directs to my Facebook page. The layout looks like this: ...

Aligning CSS Divs with Changing Content

Can someone help me with a CSS and DIV tags issue? I have a dynamic webpage and need guidance on creating a container DIV. Depending on the scenario, this container DIV will either hold one DIV with 50% width that needs to be centered or two side-by-side D ...

Retrieving POST request headers in Nightmare JS following a click() function execution and a wait() function delay

When I navigate a page, I input something in a field using type(), click on a button with click(), and then wait for an element to appear using wait(). I am interested in retrieving all the headers associated with the POST request that is triggered after ...

Searching for a column fails to yield any results after altering the value in

Here is a snippet of code that I am working with: <!DOCTYPE HTML> <html lang="en"> <head> <title> Exact matches overview </title> <script type="text/javascript" src="/static/s ...

Troubleshooting tips for optimizing Opera and Internet Explorer performance

I'm currently on the hunt for solutions or techniques to debug my jquery script specifically under the Opera/IE browser. It appears that the ajax $.post() request is either not being sent at all, or it's being sent to the wrong address, among oth ...

Directing JSON POST Request Data to View/Controller in a Node.js Application

Currently, I am working on a project hosted on a local server at http://localhost:3000/. This server receives a post request from another server in the following manner: return requestLib.post({ url: 'http://localhost:3000/test', timeout ...

Using @media queries for mobile and desktop: preventing desktop from displaying mobile styles

I have been working on redesigning a website for mobile using only CSS. I set up media queries to deliver the appropriate CSS for desktop and mobile devices. However, I noticed that when resizing the browser to under 855 pixels, some of the mobile CSS is ...

Creating responsive modal windows in Vue and Laravel to dynamically adjust to the screen size

I am currently working with a modal window code snippet. <transition name="modal" @close="showModal = false"> <div class="modal-mask" style=" width: 90% !important;"> <div class="modal-wrapper"> < ...

class-validator: ensures the correct number of digits are present in numeric values

Seeking assistance on validating the number of digits for numeric values using class-validator. Specifically, I want my entity to only accept numbers with 6 digits for a certain property. For example: const user1 = new User(); user1.code = 123456 // should ...

Transform a div's style when hovering over another div using absolute positioning without repetition

I am facing an issue with multiple divs positioned absolutely on top of a primary div with relative positioning. I want one specific div to change its background-color when hovering over another div within the same parent div. Though I know about ad ...

How do I incorporate Spotify into my mobile app to enable seamless background music playback?

Currently engaged in a mobile app project that utilizes react-native for the frontend and nodeJS for the backend. The main objective is to enable users to seamlessly play Spotify songs directly within the app, even in the background. This enhancement will ...

Ways to manage an element that has been loaded using the load query function

After implementing the query load function to update posts on the homepage, I was able to display the most up-to-date posts. However, a new issue arose: Whenever I updated all posts without refreshing the entire page, I needed a way to control the element ...

Struggling to make align-content function properly in a CSS flexbox layout grid

Utilizing CSS flexbox for designing a grid of items that wrap on resizing the page. Wanting to achieve a horizontally centered grid, successfully done with justify-content: center;. However, facing an issue where the last box in some cases is centered inst ...