tags:

views:

5201

answers:

4

Hi everyone!

I have a table and each of its td has a unique id that corresponds to some time intervals (0800 til 0830... 0830 til 0900 and so on). And I have an input text where the user will type the time intervals the they want to block. And if they type an interval that doesn't exist in my table, in other words, if they type an interval that doesn't correspond to any of my td's id's, I want to show an alert saying something like "this interval is not available for blocking".

But, I'm having difficult in find this id. I'm doing this:

var horaInicial = $("#horaInicial").val().split(':')[0] + $("#horaInicial").val().split(':')[1]; // this is remover the ":" from a formatted hour

var verificaHorario = $("#tbIntervalos").find("td").attr("id", horaInicial);

But this "verificaHorario" is actually setting all my td's to this horaInicial id.

So, how can I find an id in my table and if it doesn't exist show some alert.

thanks!!

+1  A: 

As all html ids are unique in a valid html document why not search for the ID directly? If you're concerned if they type in an id that isn't a table then you can inspect the tag type that way?

Just an idea!

S

Simon
+2  A: 

This

var verificaHorario = $("#tbIntervalos").find("#" + horaInicial);

will find you the td that needs to be blocked.

Actually this will also do:

var verificaHorario = $("#" + horaInicial);

Testing for the size() of the wrapped set will answer your question regarding the existence of the id.

kgiannakakis
+7  A: 

If you're trying to find an element by id, you don't need to search the table only - it should be unique on the page, and so you should be able to use:

var verificaHorario = $('#' + horaInicial);

If you need to search only in the table for whatever reason, you can use:

var verificaHorario = $("#tbIntervalos").find("td #" + horaInicial)
Jesse Rusak
+2  A: 

I don't know if this solves your problem but instead of:

$("#tbIntervalos").find("td").attr("id", horaInicial);

you can just do:

$("#tbIntervalos td#" + horaInicial);
cletus