Trouble with Clicking to Show Content

<div id="uniqueDiv">
   <button id="uniqueButton1" class="uniqueClass">Hit</button>
   <button id="uniqueButton2" class="uniqueClass">Run</button>
</div>

<div id="uniqueDiv2">
<p id="uniqueId1"></p><!-- points -->
<p id="uniqueId2"></p><!-- strikes -->
</div>
<div id="uniqueDiv3">
    <button id="uniqueButton3" class="uniqueButtonClass">Score</button>
</div>
<script>
document.getElementById("uniqueButton1").onclick=function(){
     document.getElementById("uniqueDiv").style.display="none";
     document.getElementById("uniqueDiv2").style.display="block";
     document.getElementById("uniqueDiv3").style.display="block";
}
</script>

I have styled the display property of uniqueDiv3, uniqueDiv2 to none and the display property of uniqueDiv to block in css. When I click on the "Hit" button, I want uniqueDiv3 and uniqueDiv2 to appear while uniqueDiv disappears. However, the functionality does not seem to work correctly.

Answer №1

<div id="div4">
   <button id="14" class="b1">Bat</button>
   <button id="15" class="b1">Bowl</button>
</div>

<div id="div3">
<p id="id4">p id4</p>
<p id="id5">p id5</p>
</div>
<div id="div1">
    <button id="id7" class="c1">Defence</button>
</div>
<script>
var ct = 0;    
document.getElementById("14").onclick=function(){
     console.log('id 14 clicked');
     document.getElementById("div4").style.display="none";
     document.getElementById("div3").style.display="block";
     document.getElementById("div1").style.display="block";

}
</script>

It is suggested to utilize console.log(); for testing purposes as it aids in debugging.

For instance, in the provided code snippet, the console will display "id 14 clicked" when the user clicks on the "Bat" button.

An error was identified in your code where incorrect id names were being used, such as id14 and 14.

Answer №2

To make the necessary adjustment, simply switch your selector from id14 to 14, like in the code snippet below.

<div id="div4">
   <button id="14" class="b1">Bat</button>
   <button id="15" class="b1">Bowl</button>
</div>

<div id="div3">
<p id="id4"></p><!-- score -->
<p id="id5"></p><!-- balls -->
</div>
<div id="div1">
    <button id="id7" class="c1">Defence</button>
</div>
<script>  
document.getElementById("14").onclick=function(){

     document.getElementById("div4").style.display="none";
     document.getElementById("div3").style.display="block";
     document.getElementById("div1").style.display="block";

}
</script>

Answer №3

Your issue might be that you forgot to specify the onclick trigger when creating your buttons and did not define the function to retrieve your elements. Check out this simple example of a button with the onclick event and the function it triggers.

This example is courtesy of a well-known coding resource, w3schools.com. Hopefully, it points you in the right direction towards resolving your problem.

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

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

Passing data from one component to another in Vue: A database perspective

I'm struggling to pass the id of a clicked button from MovieTable.vue to the WatchedForm.vue component. The WatchedForm.vue component is responsible for updating the data in the database based on the provided id, which corresponds to the Movie_id in t ...

Enhance your slideshows with React-slick: Integrate captivating animations

I recently built a slider using react slick, and now there is a need to adjust the transition and animation of slides when the previous and next buttons are clicked. I received some advice to add a class to the currently active slide while changing slide ...

Incorporate ng-click as an attribute in AngularJS

Is there a way to include ng-click as an attribute? For example, imagine I want to add ng-click if my <li> has the class someClass: angular.element(root.querySelector('li.someClass').attr({'ng-click': 'someFunc()'}); ...

Looking for a jQuery Gantt Chart that includes a Treeview, Zoom functionality, and editing capabilities?

I am in need of integrating a dynamic Gantt Chart tool into a room booking solution using jQuery. The Gantt chart should be highly interactive, allowing for drag-and-drop functionality, a tree view on the left to group tasks, and a zoom feature for adjusti ...

Identifying the pressing of the enter key in an input field

Currently, I am developing an innovative IFrame browser that features an omnibox (identified as "address") and a "GO" button (identified as "submit"). One of the key challenges I'm facing is detecting when the user inputs information and hits the Ente ...

Using Vuejs to dynamically render raw HTML links as <router-link> elements

Hey there, I'm just getting started with VueJS and I've been doing some interesting experiments. I created a backend service using Python/Flask that provides me with a string of HTML code containing many tags. My goal is to render this HTML insid ...

Creating a Flot Bar Chart that displays non-stacking values

I am new to using Flot for creating charts. Currently, I have a bar chart displayed below: https://i.stack.imgur.com/RSumf.png Here is the code snippet I utilized to generate this chart: $.getJSON('chartBar.json', function(graphDataBar){ $ ...

Tips for integrating CSS3 into Android WebView

Currently, I am working on an Android application that utilizes HTML5. My goal is to implement element rotation using CSS3 transformations. However, I need to ensure that the app remains compatible with Android 2.3. What would be the most effective metho ...

Dealing with the overflow issue on Android 4.1 using position: relative and position: absolute

I have a question regarding creating a dynamically filling rounded button. I have successfully created an inner div inside the button with absolute positioning at the bottom. It works perfectly on Chrome webview, but I'm facing issues on Android 4.1. ...

Is there a way to automatically redirect the main html page to a different html page upon logging in?

I have created a main page in HTML with a login box that displays a message saying "Login successful" or "Login failed" based on whether the password entered is 8 characters or more. The validation function for this works correctly, but after successfully ...

Incorporating Angular 2 Templates: Exploring how to bind keydown.arrow event to the entire div rather than just specific

I am currently working on a simple navigator component that includes buttons and a date-picker subcomponent. My goal is to be able to bind keydown.arrowleft etc. for the entire div, which has a CSS style of 100% height and width. Below is the template stru ...

What is the best way to transmit several data fields to a PHP page using Ajax?

Having an issue where I need to send 2 posts to my author PHP page using AJAX. Here's my original code: $.ajax({ type: "POST", url: "includes/get_competitions.php", data:'sport=<?php echo $_GET['sports']; ?>', success: func ...

Switching CSS styles - seeking a smoother integration

I have successfully implemented a JS CSS switcher, which works brilliantly. However, I would like it to function more seamlessly. When opening a new page on the site, the default CSS style sometimes flickers briefly before the selected CSS style is reappli ...

CSS not displaying properly

I've been working on a React.js web app with Material UI components. The challenge I'm facing is with one specific component that handles the website management and displays a preview of custom content. This website, unlike the rest of the app, u ...

Does Google Cloud CDN not store data from the storage bucket?

I've been experimenting with combining a Load Balancer, Cloud Storage, and CDN for the past few weeks. Unfortunately, I have not been successful in getting it to work as expected. I uploaded some static files (2 jpgs, svgs, and css files) to a multi- ...

HTML5 Slideshow with Smooth Image Transitions

So, I have created an HTML5 image slideshow and it's working perfectly on my localhost. However, I am puzzled as to why there is no transition effect within the slideshow. I was expecting something like fading out the first picture and then having the ...

Leveraging an array retrieved through JQuery AJAX for auto-complete data in materializecss

I am delving into materializecss for the first time, aiming to enhance its auto complete feature by incorporating an array of options obtained from a database. Unfortunately, my attempts have not been successful. After scouring through Stack Overflow and o ...

Verify the search input to see if the value matches in the array and display the corresponding response

As a beginner in Javascript, I have recently started learning the language in the past few weeks. What I am looking for: I want to create a search bar where users can enter their postcode and then the script will search through an array to find if the po ...

Trouble centering a list within a parent div using CSS

Even though this issue seems straightforward and many others have asked similar questions on the site, I find myself stuck after reading through several solutions and attempting various fixes. Why is this happening? My problem involves centering a navigat ...

The jQuery .hasClass() function does not work properly with SVG elements

I am working with a group of SVG elements that are assigned the classes node and link. My goal is to determine whether an element contains either the node or link class when hovering over any of the SVG components. However, I am encountering an issue where ...