Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an element's `id` attribute

Can you get the id attribute of a html tag using jQuery or without?

For example:

<ul id="todo" />

How can I get the id, without using jQuery("#todo")?

Is there a way for that? Will attr() work?

like image 457
justin Avatar asked Sep 05 '25 16:09

justin


2 Answers

You can use attr('id') in jQuery or the id property (or getAttribute('id')) on the native DOM element.

like image 194
alex Avatar answered Sep 07 '25 18:09

alex


The way I see it, you most probably need to attach a selector class to the ul so that you can use it to get the id:

<ul id="todo" class="todoclassname" />

then, you can get the id by doing:

$(".todoclassname").attr("id");
like image 23
John Gathogo Avatar answered Sep 07 '25 17:09

John Gathogo