Modifying the display property of an element using JavaScript

Hello, I'm encountering an issue with a section of my javascript code. I am attempting to make the #showAddress element display as block when the deliverservice radio button is clicked or checked. I have tried searching for solutions on Stack Overflow but many of them are too advanced for me since I am just starting out with JavaScript.

<label>Delivery Type</label>
    <input type="radio" name="delivery" value="Pickup" id="pickupService">Pickup
    <input type="radio" name="delivery" value="Deliver" id="deliverService">Deliver
</p>

<div id="showAddress">
<p>
<label for="deliveryAddress">Delivery Address</label><br>
<input type="text" name="deliveryAddress" id="deliveryAddress" placeholder="Enter street number + name"><br>
    <input type="text" name="deliverySuburb" id="deliverySuburb" placeholder="Enter your [suburb], [state]"><br>
    <input type="text" name="deliveryAddress" id="deliveryPostcode" placeholder="Enter your postcode"><br>
    <a href="#" id="sameAddress">Billing address same as Above</a>
</div>
<p>
    <label for="billingAddress">Billing Address</label><br>
    <input type="text" name="deliveryAddress" id="billingAddress" placeholder="Enter street number + name"><br>
    <input type="text" name="deliverySuburb" id="billingSuburb" placeholder="Enter your [suburb], [state]"><br>
    <input type="text" name="deliveryAddress" id="billingPostcode" placeholder="Enter your postcode"><br>
</p>
<p>

I suspect the issue lies within my JavaScript code, which consists of two functions. The first function is my initialise function that is invoking the second function below.

var showDelivery = document.getElementById("deliverService");
showDelivery.onclick =  showAddress;

function showAddress() {
    document.getElementById("showAddress").style.display= 'block';
}
#additionalInformation, #showAddress {
    display: none;
}

Answer №1

I recommend updating the following line:

showDelivery.onclick =  showAddress;

to a more modern approach like this:

showDelivery.onclick = function(){showAddress();};

Answer №2

Instead of cluttering your JavaScript code, you have the option to set the onclick listener directly on the input element in the HTML itself. This way, you can call the JavaScript function from the HTML element.

<input type="radio" name="delivery" value="Deliver" id="deliverService" onclick="showAddress()">Deliver

function showAddress() {
    document.getElementById("showAddress").style.display= 'block';
}
#additionalInformation, #showAddress {
    display: none;
}
<label>Delivery Type</label>
        <input type="radio" name="delivery" value="Pickup" id="pickupService">Pickup
        <input type="radio" name="delivery" value="Deliver" id="deliverService" onclick="showAddress()">Deliver

    <div id="showAddress">
    <p>
    <label for="deliveryAddress">Delivery Address</label><br>
    <input type="text" name="deliveryAddress" id="deliveryAddress" placeholder="Enter street number + name"><br>
        <input type="text" name="deliverySuburb" id="deliverySuburb" placeholder="Enter your [suburb], [state]"><br>
        <input type="text" name="deliveryAddress" id="deliveryPostcode" placeholder="Enter your postcode"><br>
        <a href="#" id="sameAddress">Billing address same as Above</a>
        </p>
    </div>
    <p>
        <label for="billingAddress">Billing Address</label><br>
        <input type="text" name="deliveryAddress" id="billingAddress" placeholder="Enter street number + name"><br>
        <input type="text" name="deliverySuburb" id="billingSuburb" placeholder="Enter your [suburb], [state]"><br>
        <input type="text" name="deliveryAddress" id="billingPostcode" placeholder="Enter your postcode"><br>
    </p>

EDIT: Here is an example of using addEventListener

document.getElementById("deliverService").addEventListener("click", function() {
        document.getElementById("showAddress").style.display= 'block';
});

document.getElementById("deliverService").addEventListener("click", function() {
    document.getElementById("showAddress").style.display= 'block';
});
#additionalInformation, #showAddress {
    display: none;
}
<label>Delivery Type</label>
        <input type="radio" name="delivery" value="Pickup" id="pickupService">Pickup
        <input type="radio" name="delivery" value="Deliver" id="deliverService">Deliver

    <div id="showAddress">
    <p>
    <label for="deliveryAddress">Delivery Address</label><br>
    <input type="text" name="deliveryAddress" id="deliveryAddress" placeholder="Enter street number + name"><br>
        <input type="text" name="deliverySuburb" id="deliverySuburb" placeholder="Enter your [suburb], [state]"><br>
        <input type="text" name="deliveryAddress" id="deliveryPostcode" placeholder="Enter your postcode"><br>
        <a href="#" id="sameAddress">Billing address same as Above</a>
        </p>
    </div>
    <p>
        <label for="billingAddress">Billing Address</label><br>
        <input type="text" name="deliveryAddress" id="billingAddress" placeholder="Enter street number + name"><br>
        <input type="text" name="deliverySuburb" id="billingSuburb" placeholder="Enter your [suburb], [state]"><br>
        <input type="text" name="deliveryAddress" id="billingPostcode" placeholder="Enter your postcode"><br>
    </p>

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

Ways to display title attributes when focused using jQuery?

Typically, title attributes in all browsers only appear when the mouse hovers over them. I am looking to also display them when users are focused on them via keyboard navigation. Unfortunately, without using JavaScript, this cannot be achieved solely throu ...

Tips for creating a textarea element that resembles regular text format

I am working on creating an HTML list that allows users to edit items directly on the page and navigate through them using familiar keystrokes and features found in word processing applications. I envision something similar to the functionality of To achi ...

I am struggling to find the correct way to fetch images dynamically from Cloudinary and display them as backgrounds. How should I go about implementing this feature successfully

I have been trying to figure out why I am unable to retrieve the image from cloudinary. My goal is to use the image as a background when posting, but it seems like I am not fetching the image correctly. function Post({post}){ return( <div ...

Why is my webpage attempting to refresh every time I send an AJAX request?

I have been struggling to send a form using AJAX, but no matter how many times I use e.preventDefault(), the page still tries to reload. Here is my HTML: <form action="/user/changeProfileData" id="edit-profile_form" method="post"> <ul class=& ...

What is the method for implementing two-way binding on a checkbox in Angular?

Within my accordion, I have a series of options in the form of checkboxes. Users are able to select these checkboxes, but I am seeking a way to pre-select certain checkboxes based on specific conditions. The challenge arises when these conditions are deter ...

Implementing Angular 4 to fetch information from a JSON file

As a beginner in Angular, my current task involves loading data from a JSON file upon click, which I have successfully achieved so far. However, I am facing an issue where I'm unable to load the first JSON object before clicking, meaning that I want ...

Choose just one button from the variety of buttons available

I am facing an issue with a component that contains multiple cards, each with its own button. Below is the code for this component: import React, { useState, useEffect } from "react"; import "./App.css"; import { Card, Container, Row, C ...

I am looking to pass several parameters through the post method by utilizing ajax

Can someone assist me with this code? I am trying to pass the item ID, stock quantity, and price using AJAX in a POST method. Any help would be greatly appreciated. Thank you in advance! function updateItemDetails(itemId, sellingPrice, stockQty){ v ...

Create a variety of unique images without using the same one more than once

I'm currently developing my first Javascript game, but I've hit a roadblock.. I am trying to create a game that displays random images without repeating them. Each image should be accompanied by a random number between 4 and 10. The code I have w ...

Tips for placing a searchbox on top of a carousel?

I need to create a search box that overlays on top of a carousel image. I currently have the search box positioned absolutely, but it's not responsive as it doesn't stay within the carousel as the width decreases. How can I make it responsive whi ...

Utilizing Firebase in place of .json files for the AngularJS Phonecat application

I am currently exploring firebase and attempting to retrieve data using this service from firebase instead of json files. However, I have encountered some challenges in getting it to function properly. This is inspired by the angularjs phonecat example .f ...

How to prevent users from selecting certain options using angular's ng-options

As per the Angular documentation ng-options guidelines I tried to implement this piece of code: <select ng-model="model" ng-options="item.CODE as item.NAME disable when item.DISABLE for item in list" id="foo" name="foo" ng-change="change()"> Howe ...

Passing Object Data from Child to Parent Component in React and Leveraging its Functionality

After performing a date calculation, I stored the values of year, month, and day in an object. Now, my goal is to send this object to the parent component App.js, and then pass that data to another child component named Modal.js as a prop. I want to displa ...

Extract data from THREE.js computed textures using GPUComputationRenderer

Experimenting with the GPUComputationRenderer on a customized version of this three.js example, I have been working on adjusting boid interactions using GPU shaders to handle, retrieve, and manipulate boid position and velocity data. I have reached a poin ...

Looking for a way to sort through data using the current time as a filter

In my possession is an object presented below: const user = { id: 3, name: "Clark Kent", mobileNumber: "1234567892", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385d40595548545d674d4b ...

Implementing dynamic ID routes in React Router using JSON data

As a beginner in React, I have been working hard to improve my skills every day. However, I am currently facing a challenge with creating dynamic routes using JSON characters (specifically from Dragon Ball Z). Although my routes are set up correctly, I wo ...

Ways to showcase an alert or popup when clicking?

I am utilizing a date picker component from this site and have enabled the 'disablePast' prop to gray out past dates preventing selection. Is there a way to trigger an alert or popup when attempting to click on disabled days (past dates)? Any sug ...

The initial render of Keen-slider in NextJS can sometimes encounter difficulties when using server-side rendering

While incorporating the keen-slider library v5.4.0 into my nextJS project with TypeScript, I encountered a peculiar issue. The error arises when the keen-slider is initially loaded for the first time, resulting in mismatched transform types causing items ...

Tips for displaying a React component with ReactDOM Render

_Header (cshtml) <div id="Help"></div> export default class Help { ReactDOM.render( <Help/>, document.getElementById('Help') ); } Help.js (component) } My objective is to di ...

Incorporating '@vite-pwa/nuxt' into a nuxt 3 project leads to hydration issues

I have a Nuxt 3 website that I want to make PWA enabled. To achieve this, I am utilizing '@vite-pwa/nuxt'. However, after adding the package and enabling PWA in my nuxt.config.ts file, I encountered two issues: Hydration errors occur when refre ...