<div id="test"></div>
<script>
$(document).ready(function() {
alert($('#test').id);
});
</script>
Why doesn't the above work, and how should I do this?
<div id="test"></div>
<script>
$(document).ready(function() {
alert($('#test').id);
});
</script>
Why doesn't the above work, and how should I do this?
If you want to get an ID of an element, let's say by a class selector, when an event (in this case click event) was fired on that specific element, then the following will do the job:
$('#test').attr('id')
In your example:This will finally solve your problems:
lets say you have many buttons on a page and you want to change one of them with jQuery Ajax (or not ajax) depending on their ID.
lets also say that you have many different type of buttons (for forms, for approval and for like purposes), and you want the jQuery to treat only the "like" buttons.
here is a code that is working: the jQuery will treat only the buttons that are of class .cls-hlpb, it will take the id of the button that was clicked and will change it according to the data that comes from the ajax.
code was taken from w3schools and changed.
Since the id is an attribute, you can get it by using the
attr
method.This can be element id , class , or automatically using even
Some checking code required of course, but easily implemented!