Tuesday, May 30, 2017

JavaScript: Change Font color and Background color randomly.

Source Code

<!-- TWO STEPS TO INSTALL COLORASSIST:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

Download Source Codes here


<HEAD>

<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
function showBGColor()
{
  if(document.clrForm.bgRandom.checked == true)
    document.bgColor = document.clrForm.bgColor.value = getRandom();
  else
  {
    if(document.clrForm.bgColor.value.search(/^#[a-f0-9]{6}$/ig) == -1)
      alert('Invalid background color');
    else
      document.bgColor = document.clrForm.bgColor.value;
  }
}
function showTXTColor()
{
  if(document.clrForm.txtRandom.checked == true)
    txtChg.style.color = document.clrForm.txtColor.value = getRandom();
  else
  {
    if(document.clrForm.txtColor.value.search(/^#[a-f0-9]{6}$/ig) == -1)
      alert('Invalid font color');
    else
      txtChg.style.color = document.clrForm.txtColor.value;
  }
}
var pool = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function getRandom()
{
  var clr = '';
  for(i=0; i<6; i++)
  {
    num = Math.round(Math.random() * (pool.length-1));
    clr += pool[num];
  }
  return '#'+ clr;
}
function saveColor(field)
{
  if(field == "bgColor")  // bgColor field
    opt = document.clrForm.bgColor.value.toUpperCase();
  else // txtColor field
    opt = document.clrForm.txtColor.value.toUpperCase();
  if(opt != '')
  {
    i = document.clrForm.saveList.options.length;
    for(j=0; j < i; j++)
    {
      if(document.clrForm.saveList.options[j].value == opt)
        return;
    }
    document.clrForm.saveList.options[i] = new Option(opt, opt);
    document.clrForm.saveList.selectedIndex = i;
  }
}
function putColor(field)
{
  if(field == "bgColor")  // bgColor field
  {
    if(document.clrForm.saveList.selectedIndex >= 0)
    {
      document.clrForm.bgRandom.checked = false;
      document.clrForm.bgColor.value = document.clrForm.saveList.options[document.clrForm.saveList.selectedIndex].value;
      showBGColor();
    }
  }
  else // txtColor field
  {
    if(document.clrForm.saveList.selectedIndex >= 0)
    {
      document.clrForm.txtRandom.checked = false;
      document.clrForm.txtColor.value = document.clrForm.saveList.options[document.clrForm.saveList.selectedIndex].value;
      showTXTColor();
    }
  }
}
//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<div align="center">
<form name="clrForm">
<table bgcolor="white" border="1">
<tr>
  <td align="left" valign="top"><font face="verdana" size="-1">
    <b>Background Color:</b><br>
    <input type="text" name="bgColor" value="#FFFFFF" size="8" maxlength="7" onKeyUp="document.clrForm.bgRandom.checked=false;">
    <input type="button" name="addBG" value="save ->" onClick="saveColor('bgColor');"><br>
    <input type="checkbox" name="bgRandom"> random<br>
    <input type="button" value="Display" onClick="showBGColor()">
    </font>
  </td>
  <td align="center" valign="top"><font face="verdana" size="-1">
    <b>Saved List:</b><br>
    <select name="saveList" size="2">
    </select><br>
    <input type="button" name="fillBG" value="<- put" onClick="putColor('bgColor');">
    <input type="button" name="fillTXT" value="put ->" onClick="putColor('txtColor');">
    </font>
  </td>
  <td align="right" valign="top"><font face="verdana" size="-1">
    <b>Font Color:</b><br>
    <input type="button" name="addTXT" value="<- save" onClick="saveColor('txtColor');">
    <input type="text" name="txtColor" value="#000000" size="8" maxlength="7" onKeyUp="document.clrForm.txtRandom.checked=false;"><br>
    <input type="checkbox" name="txtRandom"> random<br>
    <input type="button" value="Display" onClick="showTXTColor()">
    </font>
  </td>
</tr>
</table>
<h2 id=txtChg align=center>Make your choices above</h2>
</form>
</div>

Sample Output


Output in Video

Download Source File



No comments:

Post a Comment