Problemas para devolver un ID de elemento correcto

I've got three links which serve as sorting headings for a table, each is a member of the "sort" class. I wrote a simple jquery function that gets triggered when any "sort" class link is clicked. There's a switch case that assigns a certain column number (columnNum) to be used with the tablesorter plugin. However, right now I'm getting some weird values returned for the id of the sort classes.

$(".sort").toggle(function() { 
    // the column id's are datecaption, hourscaption, taskcaption
    var column = $(this).attr('id');
    var columnNum;

        case 'datecaption':
            columnNum = 1;
        case 'hourscaption':
            columnNum = 2;
        case 'taskcaption':
            columnNum = 3;

    var sorting = [[columnNum,1]]; 
    return false; 
}, function(){
    var sorting = [[columnNum,0]]; 
    return false;               

If I alert the column variable, I get "aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown"--which doesn't serve the rest of the function well.

¿Alguien puede decirme por qué podría estar pasando esto?

Muchas Gracias

preguntado el 10 de marzo de 12 a las 03:03

1 Respuestas

Without more code or the HTML it's hard to say. My guess is $(this) isn't what you think it is. Try adding console.log($(this)); (preferably in Firebug) click the result and you will see what $(this) is actually referring to.

Additionally, you could query the DOM for $('#aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown') and see and you should get the element $(this) se refiere a

For more information on what "this" will refer to in different situations, check out the Mozilla Developer Network:

respondido 21 mar '12, 19:03

Yes, it turns out I was looking for the encasing span's ID, not the link's. I didn't know about logging to the console before so that's an invaluable tool for the future. Thank you!! - Tim Aych

Glad I could help. Just an FYI since console.log is new to you. Make sure you take it out of 'production' code. Some browsers such as IE don't have a console defined, unless developer tools are open. So you'll get nasty undefined errors! - j-u-s-t-i-n

you can also use something like if(!console || !console.log){ console = {}; console.log = function(msg){}; } to prevent errors. There are a lot of other really useful console functions as well, - Shane

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.