mardi 12 mars 2019

Set Text Color and Text Next to Checkbox

How do I set the text color (and text) next to a checkbox?

<div style="float: left;">
    <input type="checkbox" id="idCheckbox1" onClick="myCheckbox('idCheckbox1')" value=1><label>Checkbox 1</label></input></div>
<script>
function myCheckbox(idCheckbox)
{
    var ctlCheckbox = document.getElementById(idCheckbox);
    var ctlCheckText = ctlCheckbox.parentNode.getElementsByTagName("LABEL")[0];
    var text = ctlCheckText.innerText;

    if (true == ctlCheckbox.checked)
        alert('checked');
    else
        alert('not checked');
    if (ctlCheckbox.checked == true)
       ctlCheckText.innerText = 'smurfs';
    else
       ctlCheckText.InnerText = 'gargamel';
}
</script>

Searching the internet helped a bit. I am able to get the text, and determine the check state, however setting the color or changing the text more than initially does not work.

I want to set the text blue, so I was thinking the following, which does not work, annoyingly so.

 if (true == ctlCheckbox.checked)
     ctlCheckText.innerHTML = '<span style="color:#0000FF">' + text + '</span>';
 else
     ctlCheckText.innerHTML = '<span style="color:#000000">' + text + '</span>';

Thoughts?

Here is a sample project, which I created?




Aucun commentaire:

Enregistrer un commentaire