Starts With Textarea CSS Styling Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

Starts With Textarea CSS Styling Tutorial

Starts With Textarea CSS Styling Tutorial

The other day when we presented PHP/Javascript Decoding Scrambled Words Game CSS Tutorial there were two CSS selector โ€œstarts withโ€ scenarios referenced โ€ฆ

<style>


.guess* { width: 40px; } // thanks to https://stackoverflow.com/questions/13352080/match-all-elements-having-class-name-starting-with-a-specific-string


</style>

โ€ฆ let alone what was used (ie. select element name attribute โ€œstarts withโ€ ^ syntax) and opens up interesting possibilities too โ€ฆ

<style>


select[name^='guess'] { width: 40px; } // https://stackoverflow.com/questions/41832255/css-class-name-selector-name-starts-with


</style>

Is there such a selector as โ€ฆ



textarea[value^='text at the start'] { background-color: yellow; }

? Alas, no. But there is โ€ฆ



textarea[title^='text at the start'] { background-color: yellow; }

โ€ฆ associated with the static initial HTML โ€ฆ



<textarea title='' cols=80 rows=20 onkeydown='return tuttifrutti(event);' onkeypress='return unwell(this);' onchange=well(this); onblur=well(this); id=myta></textarea>

โ€ฆ possible, and there are HTML textarea event elements which might help โ€ฆ

  • onblur
  • onchange
  • onkeypress
  • onkeydown

โ€ฆ those last two happening ahead of the textarea value being updated, and particularly useful in this scenario. Here are the relevant Javascript functions โ€ฆ

<script type=โ€™text/javascriptโ€™>


var colbit='01234567890abcdef';



function tuttifrutti(evt) {

var char = evt.which || evt.keyCode;

if (!evt.shiftKey && String.fromCharCode(char) >= 'A' && String.fromCharCode(char) <= 'Z') {

char+=('a'.charCodeAt(0) - 'A'.charCodeAt(0));

}

var sw=document.getElementById('myta').value+String.fromCharCode(char).replace(/\'/g,'');

while (sw.indexOf(String.fromCharCode(10)) != -1) {

sw=sw.replace(String.fromCharCode(10),'');

}

var colis='';

for (var icol=0; icol<6; icol++) {

colis+='' + colbit.substring(Math.max(1,Math.floor(Math.random() * colbit.length))).substring(0,1);

}

document.getElementById('dstyle').innerHTML+="<style> textarea[title^='" + sw + "'] { background-color: #" + colis + "; } </style>";

return true;

}



function unwell(tao) {

setTimeout(function(){ document.getElementById('myta').blur(); }, 20);

return true;

}



function well(tao) {

//document.getElementById('iaway').focus();

document.getElementById('myta').title=document.getElementById('myta').value.replace(/\'/g,'');

while (document.getElementById('myta').title.indexOf(String.fromCharCode(10)) != -1) {

document.getElementById('myta').title=document.getElementById('myta').title.replace(String.fromCharCode(10),'');

}

setTimeout(function(){ document.getElementById('myta').focus(); }, 20);

}


</script>

โ€ฆ which results in a textarea constantly changing dynamically with its background colour in a proofโšชof concept Textarea Value Starting With web application for the โ€œValue You Have When You Are Not Having a Valueโ€.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Not Categorised, Tutorials and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *