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

Head of Emerging Technologies

David is a change driven technologist who continually looks to adapt and expand his knowledge and understanding of his field. Over the least eighteen years David has led technology and training companies through emerging fields and technology trends helping them to understand the future and develop business opportunities. As Head of Emerging Technologies he works closely with customers and industry experts to ensure the opportunities and threats of new technology trends designing custom learning solutions to help small and enterprise organisation adapt and make the most of their people - ensuring QA is ready when our customers need to navigate the minefield of the fast moving digital landscape. His passion is in advanced web engineering principals and vendor neutral thick client design/development technologies reflected in his research, analysis and courseware development experience combined with his training delivery skills. As a technologist he is the lead instructor and syllabus author for web development technologies and specialising in Agile, DevOps, and User Experience driven approaches to developing solutions. He has authored courses such as HTML5, Responsive Web Development, User Experience, NodeJS, Javascript and jQuery.
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.