JSFiddle - React, Tailwind, and code Playground

by thecodeparadox

HTML

<div>
    <label>I agree:*</label>
    <span class="options">
        <label for="agreement_no">No</label> 
        <input type="radio" class="required authorization" value="0" id="agreement_no" name="agreement">
        
        <label for="agreement_yes">Yes</label>
        <input type="radio" class="required authorization" value="1" id="agreement_yes" name="agreement">
    </span>
</div>
<div class="description">
    Agreement Text
</div>
<div class="question">
    <label for="authorization_timestamp">Date/Time*</label>
    <input type="text" readonly="readonly" maxlength="100" class="authorization_timestamp required" name="authorization_timestamp" id="authorization_timestamp">
</div>

JavaScript

$('.authorization[id$="yes"]').on('change', function() {

    var dateStr;

    if ($(this).val() == 1) {

        var now = new Date();

        var month = now.getMonth() + 1;
        var day = now.getDate();
        var year = now.getFullYear();
        var h = now.getHours();
        var m = now.getMinutes();
        var s = now.getSeconds();

        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        h = h < 10 ? "0" + h : h;
        m = m < 10 ? "0" + m : m;
        s = s < 10 ? "0" + s : s;

        dateStr = month + "/" + day + "/" + year + " " + h + ":" + m + ":" + s
    }

    //$("#authorization_timestamp").val(dateStr);
/*$(this)
            .parent()   // go to span
            .parent()  // jump to parent div
            .next()   // .description div
            .next('.question') // .question div
            .find('.authorization_timestamp')  // find the input
            .val(dateStr); // set value*/

    $(this)
            .parent()
            .parent()
            .nextAll('.question')
            .find(".authorization_timestamp")
            .val(dateStr);


});

$('.authorization[id$="no"]').on('change', function() {

    var dateStr;
    dateStr = "";
    $("#authorization_timestamp").val(dateStr);

});