Reputation: 8115
How to get the id on an object, i know the id is hola, but i need to get it during runtime
alert($('#hola').id);
The idea is this:
<script>
(function ($) {
$.fn.hello = function(msg) {
alert('message ' + msg);
alert('is from ' + this.id); // this.id doesn't work
};
})(jQuery);
$('#hola').hello('yo');
</script>
Upvotes: 5
Views: 69408
Reputation: 344527
The most efficient approach would be:
this[0].id
this.attr("id")
takes longer to achieve the same thing because many checks are made and different codepaths are followed based on the parameter passed. Depending on how often you call the function, there could be a significant difference on, say, a mobile browser with a slow processor.
You can read more about this here.
Upvotes: 9
Reputation: 66388
You can read it as atttibute:
alert($('#hola').attr('id'));
Upvotes: 5
Reputation: 308001
Use attr()
to read attributes:
alert($('#hola').attr('id'));
Upvotes: 12