title: Javascript - setAttribute - onclick
body: <p>I am newbie in js. I have a problem with one issue.
  I want to set use my function on my parahraph to set the attribiutes..</p>
  <pre><code>&lt;body&gt;
  &lt;p id=1&gt;Paragraph 1&lt;/p&gt;
  &lt;p id=2&gt;Paragraph 2&lt;/p&gt;
  &lt;p id=3&gt;Paragraph 3&lt;/p&gt;
  &lt;/body&gt;
  
  &lt;script&gt;
  function myFunction(id) {
      document.getElementById(id).style.color = &quot;green&quot;;
  }
  initAll();
  function initAll() {
      var elements = document.getElementsByTagName('p');
      var n;
      for (n = 0; n &lt; elements.length; ++n) {
          elements[n].setAttribute(&quot;onclick&quot;, &quot;myFunction(%d)&quot;, n);
      }
  
  }
  &lt;/script&gt;
  </code></pre>
  <p>In the output I have something like that:
  <a href="https://i.stack.imgur.com/ixkeK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ixkeK.png" alt="output" /></a></p>
  <p>I can't provide the references of variable n.</p>
  <p>Maybe is there better way to set the attribiutes?</p>