Online source(s):
Category:American DJsQ:
Get ID of button clicked in dynamic created class
I am trying to get the ID of the button in class that was clicked, which in itself is created dynamically in a for-loop, but for some reason it does not work. This is what I have tried so far.
$('.add_button').click(function() {
$(this).attr("id");
alert(this.id);
});
I have even tried to follow the example from this page (How to get the id of the element that fired the event)
But it still will not work, can someone see what I'm doing wrong?
EDIT: I know that I can simply use $(this).closest('.block') to get the ID of the parent block which has been clicked. But I still would like to know why this does not work.
A:
this does not refer to the clicked element, you want $(this) which will refer to the clicked element.
You also need to prevent the default behaviour, as.click() is a shortcut for.bind('click').
$('.add_button').click(function (e) {
e.preventDefault();
$(this).attr("id");
alert($(this).attr("id"));
});
Harbor Springs, Michigan - An airport, a conference center, a Marine Corps base, a community college, a small business center and a yacht basin are some of the amenities that Harbor Springs and Charlevoix residents may have to get used to.
Mayor Sean Shaw said his city wants to develop an "airport of the future."
"We're trying to get Michigan to feel more like Florida," Shaw said. "This is a great spot for people to come."
He said Harbor Springs was chosen because of its proximity to Lake Michigan.
"We have direct access to the waterway," Shaw said.
He said the region is "uniquely situated" for development.
"You're right here in Michigan," Shaw said. "You have two big tourist attractions, Charlevoix and Mackinac Island."
Shaw said the area also attracts people who live in other parts of Michigan.
"It's all about the lake," he ac619d1d87
Related links:
Comments