I am trying to make a loop in my document.ready event so that I can have dry code. However the click event returns undefined when I create it via the loop but works fine when I declare each document.ready event singularly.
<script>
var $a = jQuery.noConflict();
$a(document).ready(function () {
$a(".nav-1-1").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-1").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-2").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-2").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-3").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-3").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-4").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-4").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-5").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-5").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-6").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-6").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-7").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-7").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-8").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-8").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-9").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-9").find("a").attr("href");
});
});
$a(document).ready(function () {
$a(".nav-1-10").css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-10").find("a").attr("href");
});
});
</script>
Here is the loop I am trying to make to simplify the above code:
<script>
var $a = jQuery.noConflict();
$a(document).ready(function () {
for (var i=1; i<11; i++) {
$a(".nav-1-1"+i).css('cursor','pointer').click(function(event) {
window.location.href = $a(".nav-1-1").find("a").attr("href");
});
}
});
</script>
I have corrected the above loop as Trevor pointed out I had forgotten to include the i variable.