Dealing with checkbox's checked and unchecked events using JavaScript

I am facing a challenge with a read-only textbox used for date input from a JavaScript date picker. Additionally, I have a gridview with checkboxes in the item template. My goal is to have the corresponding date filled in the textbox when a user clicks on a checkbox in the grid. If they uncheck the box, the textbox should be cleared. Furthermore, if the textbox already contains a date and the user selects a date from the date picker, the selected date should replace the existing one in the textbox and the checkbox selection should be cleared. I'm unsure how to manage all of these interactions. Can anyone provide guidance?

Answer №1

<p>Here is an example of a checkbox with associated JavaScript function:</p>

<input type="checkbox" id="check_box_id" onclick="toggleCheckbox(this)" />

<script type="text/javascript">
function toggleCheckbox(checkbox){
  var isChecked = checkbox.checked;
  if (isChecked){
    // Perform actions when checkbox is checked
  }else{
    // Perform actions when checkbox is unchecked
  }
}
</script>

Answer №2

Within a GridView, each checkbox and text box has a unique identifier. This can be seen in the following example:

For row 1, the checkbox id is similar to: gridname checkboxname_1 The text box id for row 1 looks like: txtname checkboxname_1

For row 2, the checkbox id is structured as: gridname checkboxname_2 And the text box id for row 2 appears as: txtname checkboxname_2

To create an onclick event for the checkbox that passes both the id and the value of the checkbox, follow this format:

onclick(this.id, checked/unchecked).

On the JavaScript side, extract the passed id (e.g., gridname checkboxname_1) and use the substring method to determine the row number (extract the last number such as 1 or 2).

Then, based on this number, construct the corresponding text box id by appending it to txtname checkboxname_. This will give you access to the current text box element.

You can then utilize document.getElementById(resultantid) to manipulate the text box when the checkbox value changes. Note that the checkbox status is also being passed, allowing you to apply conditional logic. For instance:

if(status === 'checked') {
    getElementById(resultantid).clear();
}

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

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

I'm experimenting with incorporating images into a card component using the map function in JavaScript. All aspects of the card are functioning properly except for the image

import React from 'react' import Card from '@material-ui/core/Card'import CardMedia from '@material-ui/core/CardMedia'; import CardContent from '@material-ui/core/CardContent'; import {makeStyles} from '@materia ...

Tips for verifying the rendered view post data retrieval from an API in Vue JS

Having trouble retrieving data from the API using Vue JS and printing the page? After fetching the data, some elements may not render completely when trying to print, resulting in blank data being displayed. While using a setTimeout function may work for s ...

Erase jQuery from the text

I am struggling with splitting or removing text from a filename. For example, if I have filenames like: 200726100_50-0002.JPG 230514008_60-0001.JPG The desired result should be: 230514008_60.JPG 200726100_50.JPG Am I not using the split function cor ...

Activate the drop-down menu in Angular 6 by hovering over it with your mouse

I am just beginning my journey with Angular 6 and Bootstrap. Currently, I am working on a Navigation bar that consists of 3 navigation items. One of the nav items is called "Store", and when a user hovers their mouse over it, I want to display a mega menu ...

The 'import type' declaration cannot be parsed by the Babel parser

Whenever I attempt to utilize parser.parse("import type {Element} from 'react-devtools-shared/src/frontend/types';", {sourceType: "unambiguous"}); for parsing the statement, I come across an error stating Unexpected token, exp ...

Iterate through the classes for updates rather than focusing on individual id fields

I am currently working on creating a function that can refresh data with an AJAX call for multiple instances of a class within a single page. The function functions perfectly when there is only one instance on the page: $(document).ready(function() { ...

Using VueJS to create buttons by nesting data arrays

Struggling with creating buttons in multiple modal windows by looping over an array. The initial buttons work fine, but the nested array of options doesn't show up in the new modal window when clicked. How can I make the optional buttons appear when t ...

Difficulty encountered when attempting to utilize keyup functionality on input-groups that are added dynamically

I've exhausted all available questions on this topic and attempted every solution, but my keyup function remains unresponsive. $(document).ready(function() { $(document).on('keyup', '.pollOption', function() { var empty = ...

Transmit information from node.js to the frontend utilizing only raw JavaScript

Seeking guidance on sending data object from a Node.js server to a JS file for frontend use. In the main.js file, DOM manipulation is performed. The following request is made: let dataName = []; let request = async ('http://localhost:3000/') =& ...

What is the best way to adjust the size of an image as I navigate down a webpage?

I've been working on designing a navigation bar for a website, but I'm running into some issues. My goal is to have the logo shrink as the user scrolls down the site. I've experimented with webkit animations and various javascript/jQuery fun ...

Stop allowing users to place periods before their nicknames on Discord servers (Programming in discord.js with a Discord Bot)

I have been working on a script to identify when a user alters their name on Discord by adding a period at the beginning, such as changing "bob" to ".bob". The goal is to prevent this change and keep it as "bob". if (user.nickname.startsWith(".")) { ...

What is the best way to retrieve my files stored on my express server?

I have successfully uploaded my images to my server using Multer, but now I am struggling to serve that file. When I try to access the path: http://localhost:3001/public/backend/public/uploads/user-admin-1556247519876.PNG, I receive a 404 error. I believe ...

Searching for nested objects while adhering to JSLint guidelines can be achieved by following

I have a structured object and I'm trying to locate the product using its ID. 0 :{ id: 0, title: 'xxxx', url: "www.test.com" quantity: 100 }, 1 :{ id: 10, title: 'xxxx', url: "www.test.com" ...

What is the best way to update data by making API calls within store.js using Vuex?

I am in the process of integrating vuex into my project. I have familiarized myself with mutations and actions for updating state properties. My main inquiry is regarding the most secure and effective method to update state components by retrieving data fr ...

Creating a JSON PHP array structure within an ASP.NET 2008 environment

Is there a way to json serialize this array structure using dotnet 3.5? <?php $response = array( 'file_version' => 2, 'files' => array( array( 'file_name' => 'tes ...

The spinning loading wheel on Firefox persists even after submitting the iFrame

I have encountered a strange issue with Firefox that I can't seem to figure out. In my system, AJAX is used to send data to a PHP API. However, when file uploads are involved, Firefox does not use XMLHttpRequest() and instead reverts to submitting the ...

Using the async.waterfall function in an Express application

Query: I'm encountering an issue with my express.js code where, upon running in Node.js, an empty array (ganttresult) is initially displayed. Only after refreshing the browser do I obtain the desired result. To address this problem, I have attempted ...

Obtaining values from event listeners in Vue.js across different methods

Looking for guidance on the code snippet provided below. Is there a way to assign the value of "e.detail.name" to "nodeName," and then access it in a different method within the component for an API request? data() { return { nodeName: '' ...

JavaScript function not being executed after AJAX response in HTML

There seems to be a problem with my jQuery code. After receiving an html response from an ajax call and prepending it to a div, a div within that received html is not triggering a function in my external javascript file. In the index.php file, I have incl ...