Updates from QA Training

Selecting Secrets of the Javascript Master

Allow my zen mastery of Javascript to enlighten you young Padawan. Consider this Javascript question: "Tell me my young delegate how you would select the following HTML using javascript..."


David Walker | 11 July 2012

Allow my zen mastery of Javascript to enlighten you young Padawan. Consider this Javascript question: "Tell me my young delegate how you would select the following HTML using javascript":

"<p id="intro">

"Ahh says the delegate you use:"

 document.getElementById('intro');

"What about selecting all <p> in the HTML?" Say I.

Hmmm the student ponders further.

How about:

 document.getElementByTagName('p');

"Yes I say, you are wise and skilled in the way of javascript. " Taking my best Mr Miyagi stance knowing that the young Padawan has mastered Wax on, wax off it is time to show them the true geek mastery of the untamed javascript! Taking a tiger stance I ask, "How then shall we find all paragraphs that are immediate children of a div tag?"

The strongest among them says "we would select the <div>'s then select <p>'s from the <div>'s." They are strong! I am proud! Yet there is much to learn.

"The solution you have suggested is arduous my friend, it involves nested and recursive loops and like Kane leaving the temple of Kung Fu the scars you will carry will haunt you for the rest of your days."

"Let me make you faster and stronger."

 document.querySelectorAll('div > p'); 

With a call to console.dir(); I prove that I am still the master, but our day of ultimate reckoning draws closer.

The delegate is humbled yet enlightened so I task them with one more question."The"<p id="intro"> I spoke of. I have removed its ID."I laugh an evil Kung-Fu laugh."It is the first paragraph in the document. FIND IT!" I command!" 

Suddenly the delegate leaps into action and proclaims:

 document.querySelector('p:first-of-type');

"I have realised Master Dave that the CSS3 Selectors are useable here!"

I bow, touch one clenched first to an open palm. "This is true, it is faster, more efficient and less code heavy. It makes your syntax easier to read for designer and developer."

"Then why Master Dave do we still use document.getElementById() ?" I turn with a look of cold determination."For IE7 and below do not understand these new selectors and IE 8 s flabby and weak."Turning with grim determination I proclaim,"To tame them we shall turn to the magic of jQuery!"

Then the CSS Ninjas attacked!

My name is David Walker and I train JavaScript Kung-Fu Masters for a living!


David-Walker---sq

David Walker

Portfolio Director - Digital Transformation and Emerging Technology

David is a change-driven technologist who works closely with organisations and industry experts to understand the opportunities and threats posed by new technology. David has designed multiple learning solutions that have helped organisations redefine their business strategy and culture – creating new business models that view IT as a strategic competency rather than a support function.
Talk to our learning experts

Talk to our team of learning experts

Every business has different learning needs. QA has over 30 years of experience in combining the highest quality training with the most comprehensive range of learning services, ensuring the very best fit for your organisation.

Get in touch with our learning experts to talk about how we can help.