function TestClass(strName) {
this.Name = strName;
}
TestClass.prototype.AlertValue = function (anyValue) {
console.log(this.Name + " is alerting: " + anyValue);
}
function Init() {
var objContentDiv = document.getElementById("content");
var objA = null;
var objTest = new TestClass("Tester");
// This runs the for loop using a LOCAL scope for each iteration.
(function loop(intI) {
var intX = intI;
if (intI < 10) {
// Create new A element.
objA = document.createElement("a");
// Set A display properties.
objA.style.display = "block";
objA.style.backgroundColor = "#F8F8F8";
objA.style.border = "1px solid #333333";
objA.style.padding = "10px 10px 10px 10px";
objA.style.color = "#333333";
objA.style.marginBottom = "15px";
// Create text for the link.
objA.appendChild(document.createTextNode("I should alert " + intX + " when clicked"));
// Set the href.
objA.setAttribute("href", "##");
// Set the onclick method.
objA.onclick = function () {
objTest.AlertValue(intX)
};
// Attach the A to the content.
objContentDiv.appendChild(objA);
loop(intI + 1);
}
})(0);
}
$(document).ready(function(){
Init();
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.