Displaying a checkbox within an input field

I'm attempting to incorporate a datepicker that includes the ability to disable its input field. The goal is to have the input field disabled when "Never Expire" is selected, as shown in the image below.

https://i.sstatic.net/qRC1t.jpg

Here's what I've implemented so far:

<style>
.chkbox {
    padding-right: 10px;
    font-weight: bold;
    position: absolute;
}
</style>
<input type="text" placeholder="&#xf073; Select Date" id="datepicker" autocomplete="new-datepicker" name="expireDate" style="font-family:Arial, FontAwesome" />

My questions are as follows:

  1. How can I add the "Never Expire" checkbox on the right side of the Date Picker input field?
  2. How do I disable the datepicker input field when the "Never Expire" option is selected?
  3. How can I ensure that only one option (either datepicker or checkbox value) is submitted?

Appreciate your help and insights! :)

Answer №1

I am not a fan of using absolute positioning to relocate the checkbox within the date picker input field. I believe utilizing Bootstrap input group would be a more suitable approach.

<form>
    <div class="form-group">
        <div class="input-group date-select-with-expire-option">
            <div class="input-group-prepend">
                <span class="input-group-text">
                    <i class="far fa-calendar-alt"></i>
                </span>
            </div>
            <input type="text" class="form-control date-input" name="SelectedDate"
                   placeholder="Select date" />
            <div class="input-group-append">
                <div class="input-group-text">              
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" 
                            class="custom-control-input expire-option"
                            name="DateNeverExpires" id="never-expired">
                        <label class="custom-control-label" for="never-expired">
                            Never expires
                        </label>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

Initially, it will look like this:

https://i.sstatic.net/GoFl6.png

After some CSS/SASS modifications:

.input-group.date-select-with-expire-option {
    .input-group-text {
        background-color: transparent;
    }
    
    // Handling multiple .form-control elements
    .form-control {
        &:first-of-type {
            border-left-color: transparent;
        }
        
        &:last-of-type {
            border-right-color: transparent;
        }
    }
    
    .input-group-prepend {
        .input-group-text {
            border-right-color: transparent;
        }
    }
    
    .input-group-append {
        .input-group-text {
            border-left-color: transparent;
        }
    }
}

Final result after styling changes:

https://i.sstatic.net/3C4DZ.png

To implement logic that disables the date picker input when "Never expire" is checked, jQuery can be used:

$(function() {
    $('.date-select-with-expire-option .expire-option[type=checkbox]').change(function() {
        console.info($(this));
        let $dateInput = $('.date-select-with-expire-option').find('.date-input');
        $dateInput.prop('disabled', $(this).is(':checked'));
    });
});

Visual indication when the checkbox is selected:

https://i.sstatic.net/xaWfE.png


The provided solutions can be implemented and tested in your application. Ensure that input elements have the appropriate name attribute set.


Demo: https://jsfiddle.net/davidliang2008/thbfnd2s/38/


Update: Implementing Date Range Picker

An alternative example involving the use of Date Range Picker as the date picker solution for the input field.

Date input selection through class or Id reference:

function() {
    $('.date-select-with-expire-option .date-input').daterangepicker({
        singleDatePicker: true
    });

    ...
}

https://i.sstatic.net/7DzCh.png

Demo: https://jsfiddle.net/davidliang2008/thbfnd2s/42/


Update: Implementing jQuery Date Picker

In case there are issues with jQuery date picker usage, proper installation is necessary:

  • Download jQuery UI:
  • Include jQuery UI base CSS:

Modify the code snippet implementing the date picker object:

$(function() {
    $('.date-select-with-expire-option .date-input').datepicker();
    
    ...
});

https://i.sstatic.net/4bQ1L.png

https://i.sstatic.net/wZOVH.png

Demo: https://jsfiddle.net/davidliang2008/thbfnd2s/43/

Answer №2

To display both the checkbox and date picker together, nest them within a div as a wrapper element. Then, apply absolute positioning to the checkbox and adjust its alignment using margin properties.

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

Bootstrap and AngularJS straining against each other

My page navigation and image loading are done using Angular, but now I want to integrate Bootstrap Glyphicon Components. However, the images loaded from the Angular function are not working with Bootstrap. When I remove Bootstrap from my HTML, everything w ...

When using Material UI, it is not possible to apply the text-overflow: ellipsis and overflow: hidden properties to multiple

I am new to material UI and here is the current state of my website: view reality of current website This is what I am expecting it to be: what I envision it becoming From the images above, you can see that I want the text inside the circle to be shorten ...

Is there a way to make the first Image Element within the div automatically clickable?

Is there a way to set the first image in a div as the default clicked element? I have a div with multiple images and I want the first one to be clicked by default. This is part of a React functional component that serves as a view. <div className=&quo ...

Navigate through the pages by selecting from the drop down menu, but the drop down seems to be missing on the second page

Check out this link. I'm interested in exploring each page linked to an item in the dropdown menu at the top of the website. Recently started using selenium, here's what I've been working on: Start by opening the driver Navigate to the w ...

Angular pagination with enforced ellipses using Bootstrap

I have encountered an issue with pagination on the Plnkr I created. You can check out the problem here: http://plnkr.co/edit/YQAUCg5vjcgS1BEGB4nY?p=preview. Essentially, I have a large number of pages and I have set the attribute force-ellipses = true to ...

Using Jquery Mobile to make an AJAX POST request with XML

Is it possible to use this code for XML parsing? I have successfully parsed using JSON, but there is no response from the web service. This is the status of the webservice: http/1.1 405 method not allowed 113ms $j.ajax({ type: "GET", async: false, ...

Struggling with implementing a materialize modal?

I am encountering a problem with Materialize. This time, I am trying to create a modal div, but it doesn't seem to be working. The button is created, but when I click on it, nothing happens. I have made sure to link all the necessary Materialize files ...

Adjust the background color of the dropdown menu

I'm struggling to change the background color of the property address picker dropdown from transparent to white. I've been trying different things but can't seem to get it right. Any suggestions? Additionally, I want to change the cursor to ...

The response from my post is not being displayed on my ajax form

Recently, I've encountered an issue with my Ajax form where the response value I enter in the text field doesn't show up. I'm puzzled as to why the post value is not displaying at all. reset.php <html> <head> <script src ...

Incorporate sweetalert2 into your node.js and express.js project

Trying to incorporate sweetalert2 into my Node.js and Express.js project has not been successful so far. The error message I keep encountering is: swal is not defined ReferenceError: swal is not defined Here are my configurations index.js var express ...

Only one active class is allowed in the Bootstrap Accordion at any given time

I have implemented Bootstrap's accordion on my webpage, consisting of two different sections with unique content. Upon loading the page, the active class defaults to the first element of the first section. However, if I navigate to the "Second" sectio ...

The prop type for `rows` is invalid in `ForwardRef(DataGrid)`. It was supplied as an object instead of the expected array

Hello there! I'm puzzled as to why my grid table isn't displaying data even though I can confirm that I am receiving data from the API response. I'm wondering what might be wrong with my code. Below is my current code along with the returned ...

The `finally` function in promises is failing to execute properly

Currently working with Typescript and I've included import 'promise.prototype.finally' at the beginning of my index.js file (in fact, I've added it in multiple places). Whenever I try to use a promise, I encounter the error message say ...

Displaying a webpage within a div section of another webpage by referencing it from a separate webpage

If I have three html pages named index.html, page1.html, and page2.html. Imagine that I am currently on page2.html and there is a list of items, each linking to different pages. Let's say one item links to page1.html. Is it feasible to load page1.ht ...

Is it more advantageous to create two distinct projects for the frontend and backend along with an API, or should I consolidate them into a

Asking for some guidance on a few queries I have. I've developed a backend node server with express & mongo to run specific tasks and store them in the database, along with creating an admin page using express & bootstrap. Everything is functioning s ...

What is the process for executing code on a server by clicking a button?

In my Next.js application, there is a file named dummy.js with the following content: class Dummy extends React.Component{ static async getInitialProps(ctx){ return { dummy : 'abc'}; } displayHelloWorld(params) { cons ...

Utilizing Fullcalendar 5 in conjunction with Angular: Embedding Components within Events

Recently, my team made the transition from AngularJS to Angular 12. With this change, I upgraded Fullcalendar from version 3 to version 5 and started using the Angular implementation of Fullcalendar: https://fullcalendar.io/docs/angular While navigating t ...

Dynamically linking tabbable tabs is a useful technique that allows for

I have been working on an app that organizes websites into groups displayed as tabs in a tabbable navigator using Twitter Bootstrap. The idea is that each time a new group is added, a new tab should appear. Currently, this is how it looks: The functionali ...

Issue with Masonry layout not adjusting to change in window size

Something seems to be fixed on displaying four rows, no matter the size of the window. Previously, it would adjust to three rows or fewer as the browser was resized. I recently played around with columnWidth, but reverting it back to 250 doesn't seem ...

Retrieve the text from the currently active birth control pill

Being new to this, I've looked everywhere and can't find a solution. I am trying to retrieve the text of the currently active BS pill using the following code: $('.nav-pills').on('click',function(){ ...