Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - how can I find the element with a certain id?

Tags:

jquery

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).

I have an input text where the user will type the time intervals they want to block.

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 difficulty to 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.

How can I find the id in my table and if it doesn't exist show some alert?

like image 806
André Miranda Avatar asked Mar 12 '09 12:03

André Miranda


People also ask

How do I find an element with specific ID?

getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

How do I know which ID is clicked in jQuery?

click(function() { var id = $(this). attr('id'); $container. cycle(id. replace('#', '')); return false; });

How can get li id value in jQuery?

ready(function() { $('#list li'). click(function() { alert($(this). attr("id")); alert($(this). text()); }); });

How do you check if the ID exists in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.


1 Answers

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) 
like image 149
Jesse Rusak Avatar answered Oct 18 '22 15:10

Jesse Rusak