What do โSimon Saysโ and โTic Tac Toeโ have in common, for us today?
Theyโre both games.
Yeh, well โฆ okay โฆ but apart from โTheyโre both gamesโ โฆ What do โSimon Saysโ and โTic Tac Toeโ have in common, for us today, like?
Neither of them are called Noughts and Crosses.
Well, yes, in non-Australian parts โฆ yes, well โฆ okay โฆ but apart from โTheyโre both gamesโ and โNeither of them are called Noughts and Crossesโ โฆ What do โSimon Saysโ and โTic Tac Toeโ have in common, for us today, like, web application coding wise?
They both use conspicuous repetition of identical initial consonant sounds in successive or closely associated syllables within a group of words.
Now I think weโve been Googling now โฆ well, okey dokey โฆ yes, well โฆ okay โฆ but apart from โTheyโre both gamesโ and โNeither of them are called Noughts and Crossesโ and โThey both use conspicuous repetition of identical initial consonant sounds in successive or closely associated syllables within a group of wordsโ โฆ What do โSimon Saysโ and โTic Tac Toeโ have in common, for us today, like, web application coding wise, in terms of wearing a tophat, like?
Both have had updates to their code by a balding โฆ
Righteo โฆ thatโs it โฆ apart from โTheyโre both gamesโ and โNeither of them are called Noughts and Crossesโ and โThey both use conspicuous repetition of identical initial consonant sounds in successive or closely associated syllables within a group of wordsโ and โBoth have had updates to their code by a balding โฆโ โฆ โSimon Saysโ and changesto our tictactoejsโs class
mode of use live run Firefox Scratchpad Javascript Placeholder Tutorial inspired external Javascript โTic Tac Toeโ show that (my usual line of thinking that) โฆ
โฆ can be mixed up a bit in that we used โฆ
- HTML class attribute as a controller of onclick logic via document.querySelector in โSimon Saysโ โฆ and today, weโve allowed โฆ
- HTML class attribute can be combined with HTML inline CSS onclick event logic as well
โฆ both idea concepts capable of ending up with webpages containing no ID attribute HTML? You may well ask, why is this a big deal? Weโd say here, that it is โฆ
- indicative of the flexibility of Javascript, the recognition of which we think is an important component to the thinking of web application developers โฆ as well as โฆ
- to occasionally set yourself austere restrictions (and the blog posting series with CSS but no Javascript culminating in Missing Javascript Audio on Unmute Tutorial springs to mind here) teaches you about โpinch pointsโ
What โpinch pointโ was the crucial one allowing โTic Tac Toeโ be either an ID attribute (the old approach) or class attribute (the new option for mode of use) based controller of event logic for the web application? It was the replacement of Javascript DOM โฆ
var anHTMLobject=document.getElementById('anHTMLobjectID'); // our favourite Javascript DOM type of usage
โฆ type lines of code (mostly, but not everywhere) with (an inhouse) โฆ
var anHTMLobject=documentgetElementById('anHTMLobjectID');
function documentgetElementById(inid) {
var reto=document.getElementById(inid);
if (!reto) {
var alles=document.getElementsByTagName('*');
for (var ialles=0; ialles<alles.length; ialles++) {
if (('' + alles[ialles].className).indexOf(inid) != -1) { reto=alles[ialles]; }
}
}
return reto;
}
โฆ that suits a round of code changes that wherever we had written (in the HTML written out) something like โฆ
... id="t21">
โฆ that now gets tokenized via (global variable โidvsclassโ to) โฆ
... ' + idvsclass + '="' + classy('t21') + '">
โฆ which is now a variable controlled via web browser address bar URL argumentation code as per โฆ
var idvsclass=location.search.split('idvsclass=')[1] ? decodeURIComponent(location.search.split('idvsclass=')[1].split('&')[0]) : 'id';
var exbidvsclass=location.search.split('idvsclass=')[1] ? '&idvsclass=' + decodeURIComponent(location.search.split('idvsclass=')[1].split('&')[0]) : '';
var atend='?reflexes=y' + exbidvsclass + '&between=';
โฆ in turn controlled, as far as the user is concerned, by a piece of hardcoded โโฆโ in an H1 element becoming a dropdown with ID versus class options as per โฆ
var threedots=location.search.split('idvsclass=')[1] ? "<select onchange='if (this.value.length > 0) { location.href=(document.URL.replace(\"idvsclass=\",\"idvsxclass=\") + \"&idvsclass=\" + this.value).replace(\".html&\",\".html?\").replace(\".htm&\",\".htm?\"); } '><option value=''>...</option><option value=class>class</option><option value=id>id</option></select>" : "<select onchange='if (this.value.length > 0) { location.href=(document.URL.replace(\"idvsclass=\",\"idvsxclass=\") + \"&idvsclass=\" + this.value).replace(\".html&\",\".html?\").replace(\".htm&\",\".htm?\"); } '><option value=''>...</option><option value=id>id</option><option value=class>class</option></select>";
var hcont = '<h1 align="center">You say Tic Tac Toe ' + threedots + ' I say Noughts and Crosses</h1>';
Here is another feature of HTML class attribute use that is truly different to ID use. You can have multiple classnames defined for an inline CSS defined HTML coded element. And here with Tic Tac Toe we use (a strategy that ID control is not capable of, whereby) โฆ
- the first classname part of the class HTML inline CSS definition for the table cell (td) elements is what is that cellโs ID (of the old approach) โฆ and we take the opportunity to value add (a bit) by having โฆ
- the second classname part of the class HTML inline CSS definition for the table cell (td) elements is โzโ prefixed to that above
โฆ so that we can add a little border colour razzamatazz to the Tic Tac Toe (class attribute controlled mode of use) style of the game as per โฆ
var scols=['red','olive','orange','brown','green','blue','magenta','pink','purple'];
var scolsofar=',,';
var stylebits=location.search.split('idvsclass=')[1] ? '<style> .zt11 { border:5px solid ' + randc() + '; } .zt12 { border:5px solid ' + randc() + '; } .zt13 { border:5px solid ' + randc() + '; } .zt21 { border:5px solid ' + randc() + '; } .zt22 { border:5px solid ' + randc() + '; } .zt23 { border:5px solid ' + randc() + '; } .zt31 { border:5px solid ' + randc() + '; } .zt32 { border:5px solid ' + randc() + '; } .zt33 { border:5px solid ' + randc() + '; } </style>' : '';
function randc() {
var cchoice='';
while (scolsofar.indexOf(',' + cchoice + ',') != -1) {
cchoice=scols[Math.floor(Math.random() * scols.length)];
}
scolsofar+=cchoice + ',';
return cchoice;
}
function classy(inb) {
var inbsuffix='';
if (idvsclass == 'class') { inbsuffix=' ' + 'z' + inb; }
return inb + inbsuffix;
}
Maybe youโd like to compare and contrast to the document.querySelector and class attribute aspects of the โSimon Saysโ game of the recent Simon Says Game Primer Tutorial below.
Previous relevant Simon Says Game Primer Tutorial is shown below.
One of the great things about the โnetโ is learning things off other people. Here at this blog weโve mentioned the stupendous HTML5 File API link weโve used a lot for โshape to uploadโ local disk file browsing purposes in so many web applications weโve talked about. So, thanks for that, but that File API brilliance is not all we learnt by putting that logic into play. Weโve not up until now, formalized it into a โproof of conceptโ web application, but weโve now adapted its โฆ
At HTML5 File API โฆ |
---|
|
Adapted for a Simon |
document.querySelector(โ.classyโ).addEventListener(โclickโ, function(evt) { if (evt.target.tagName.toLowerCase() == โbuttonโ) { eval(evt.target.title); } }, false); |
Letโs learn a bit more about the crucial document.querySelector method โฆ
The querySelector() method returns the first element that matches a specified CSS selector(s) in the document.
For me, and I grant you it might just be me, but it breaks the nexus in my mind that I should always be thinking โฆ
- HTML element ID attribute is associated with Javascript DOM (action) work โฆ eg. clicking a button and looking after other event logics
- HTML element class attribute is associated with CSS (styling) work
โฆ and not quite โnever the twain shall meetโ because ID CSS is commonly used by us, but generally the above principles hold for us.
The use of the document.querySelector method above turns this on its head though, to the extent that with the underlying Simon Says game HTML and Javascript and CSS doc_qshtml code โฆ
- no HTML elements needed any ID attributes โฆ and yet โฆ
- HTML button elements with a class are controlled by onclick logic instigated by that right hand Javascript code above โฆ but it had to be (a little awkwardly, we grant you) arranged that โฆ
- only one HTML button class=โclassyโ exists at any given time to allow that document.querySelector methodology to serve its purpose in the Simon Says game (which is just a bit of fun trying to succeed with what Simon Says to turn its underlying table all green)
We hope this is of interest to you too, and thanks to this useful link for some CSS ideas for button styling, and 30 Funniest Simon Says Ideas for Simon Says ideas.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.