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>
Use attr()
to read attributes:
alert($('#hola').attr('id'));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With