document.querySelectorAll() function not working on all elements
<p>I am using <code>document.querySelectorAll()</code> function to catch <code>&lt;a&gt;</code> in my list of <code>&lt;li&gt;</code> but the function is not working on all elements i have to define like this <code>document.querySelectorAll('//elements')[0]</code>, <code>document.querySelectorAll('//elements')[1]</code> if i define like this it works on element on that index only.</p>

<p>I don't think its good to define like this for all elements. Can anyone tell me please where i am wrong why its not working on all elements.</p>

<p>Here is my current code</p>

<p><strong>HTML</strong></p>

<pre><code>&lt;ul class="questions"&gt;
    &lt;li&gt;
        &lt;a href="#" class="clearfix"&gt;&lt;span class="faq_name"&gt;My Account&lt;/span&gt;&lt;span class="arrow"&gt;&lt;img src="images/plus.png"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;div class="answer_box"&gt;
            &lt;p&gt;Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. &lt;/p&gt;
        &lt;/div&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;a href="#" class="clearfix"&gt;&lt;span class="faq_name"&gt;Categories&lt;/span&gt;&lt;span class="arrow"&gt;&lt;img src="images/plus.png"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;div class="answer_box"&gt;
            &lt;p&gt;Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. &lt;/p&gt;
        &lt;/div&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;a href="#" class="clearfix"&gt;&lt;span class="faq_name"&gt;Trouble with check-in&lt;/span&gt;&lt;span class="arrow"&gt;&lt;img src="images/plus.png"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;div class="answer_box"&gt;
            &lt;p&gt;Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. &lt;/p&gt;
        &lt;/div&gt;
    &lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p><strong>JS</strong></p>

<pre><code>document.querySelectorAll('.questions li a').onclick = function() {myFunction()};
function myFunction(){
    alert('asd');
}; 
</code></pre>