JSFiddle - React, Tailwind, and code Playground

by YangHax

HTML

<input type="hidden" id="Comments__c">
    
<input type="button" onClick="showHiddenInputValue()" value="Show Hidden Input Value"/>
    
<input type="button" onClick="getCookieAndPutIntoHiddenInput()" value="Get Cookie &amp; Store in Hidden Field"/>

JavaScript

// JS Cookies for Chris Rumble

// set cookie right away
document.cookie = 'Chris = Rocks'

function showHiddenInputValue() {
    alert(document.getElementById('Comments__c').value);
}

function getCookieAndPutIntoHiddenInput() {
    var cookie = getCookie('Chris'),
        hiddenInput = document.getElementById('Comments__c');
    hiddenInput.value = cookie;
    alert('Stored "Chris" cookie in hidden field: ' + cookie);
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
    }
    return "";
}