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..
<body>
<p id=1>Paragraph 1</p>
<p id=2>Paragraph 2</p>
<p id=3>Paragraph 3</p>
</body>
<script>
function myFunction(id) {
document.getElementById(id).style.color = "green";
}
initAll();
function initAll() {
var elements = document.getElementsByTagName('p');
var n;
for (n = 0; n < elements.length; ++n) {
elements[n].setAttribute("onclick", "myFunction(%d)", n);
}
}
</script>
In the output I have something like that:
I can't provide the references of variable n.
Maybe is there better way to set the attribiutes?