views:

2115

answers:

4

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)

+8  A: 

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.

$(function(){   
    for(i = 1; i <= 31; i++){
        var d = '#days' + i;    

//       if($(d) && i == 11){            
         if(i == 11){
               $(d).addClass("date_has_event");
               //console.log("diez");
         }else{
               console.log("otro");
         }
    }
}
tj111
because d contains an id "#days" which I am using to select certain elements
Luis Armando
I know but it seems redundant. Your creating '#days11', selecting the element that matches '#days11', and seeing if it contains '11'. That would only return true when 'i == 11'.
tj111
reading more into the code, it seems logical that the 11 arbitrary, why would the 11th always be the only one with a date, and why bother looking for it if you already know that.
Jeremy B.
+1: This solution is simpler because it more easily accounts for numbers that could be subsets of other numbers (1 and 10 for example).
Joel Potter
this solution is also less performant. Forcing a javascript loop that will then do a selector each time is bypassing built in functionality to do it the "hard way"
Jeremy B.
if($(d)) will always be true. I think you meant: if($(d).length)
JPot
+3  A: 

Ok, new answer. the way you are doing this is not very "jqueryish". lets step back a bit. from what I can tell you have an html structure something like:

<div id="days1"></div>
<div id="days2"></div>
...

You are then running this against every item with a days(num) id? A better solution is this, if you want to add a class to every element with a date in it, first apply a class:

<div class="days"></div>
<div class="days"></div>

Your code can then be

$(function(){
    $(".days").each(function(i){
        if($(this).substr(4,2) == 11){
            $(this).addClass("date_has_event");
        }
    });  
});
Jeremy B.
You need $("#" + elementId) in jquery.
Joel Potter
I'm sorry what? I'm not selecting off an id, i'm selecting the classes and iterating through them. This example is fully proper jQuery
Jeremy B.
+1 If you're doing a selector in a loop you're probably doing something wrong.
Adam Lassek
+1  A: 

Since you are selecting by id, this is redundant:

if ($(d).attr("id").substr(4,2) == 11)

because the ID attribute of d is d.

Is most simple to do:

if (i == 11)
eKek0
A: 

Missing the closing bracket?

$(function() {  
    for(i = 1; i <= 31; i++) {
        var d = '#days' + i;    
        if (i == 11) {
            $(d).addClass("date_has_event");
            //console.log("diez");
        } else {
            console.log("otro");
        }
    }
// shouldn't the next line be });
}
Georg