I have a variable d that I use like this:
$(function() { for(i = 1; i <= 31; i++) { var d = '#days' + i; if ($(d).attr('id').substr(4,2) == 11) { $(d).addClass('date_has_event'); //console.log('diez'); } else { console.log('otro'); } } }
However I get the following error in firebug:
$(d).attr('id') is undefined index.html (L23) (?)()() jquery.min.js (L27) onreadystatechange()() jquery.min.js (L27) onreadystatechange()() jquery.min.js (L21) nodeName()([function(), function()], function(), undefined) onreadystatechange()()
I really don’t understand why. Does anyone know?
Edit
I’m sorry for the poor explanation I had to run, here’s what’s going on a little bit more detailed. I am generating a calendar using javascript. each td has a different id (hence the #days + i) and I am running it from 1 to 31 so I can cover the longer months. However I am getting the error I mentioned above. I am also using the jQuery library to enable me to select more easily (i.e. instead of getElementById just #days)
Why not just check if
i == 11, then do your processing on it? It would still only fire on$('#days11'). Edit: If you need to make sure the element exists as well, just slap that into the conditional.