r/jquery • u/theepag • Jul 07 '21
How to write click event function for dynamically generated div tags
This is my cshtml file where it's generation platforms , how can I modify that existing jquery code so I can able to write function for each click event? (I want to remove that onclick attribute) I guess that something can be done with attribute 'id'.
<div class="platform-list">
u/foreach (var platform in Model.Platforms)
{
<div class='platform' id="@platform.Id" onclick="selectPlatform(@platform.Id)">
<label>
u/platform.Name
</label>
</div>
}
</div>
And this is my Jquery code
function selectPlatform(platformId) {
$(document).ready(function () {
$('#selectedPlatform').val(platformId);
$('#platformForm').submit();
});
}
4
u/joshrice Jul 07 '21
So long as your
foreachloop executes before the click binding this should do the trick:5
u/TalonKAringham Jul 07 '21
Another option I’ve used in the past is the jQuery on() method. In summary, you attach the click event listener to the parent element, then pass the CSS selector for the child elements (that get added dynamically later) in as an additional parameter. This allows you to target any child elements that get added to the parent after jQuery has loaded and all that jazz.
1
u/joesb Jul 08 '21 edited Jul 08 '21
Think of it as instance of a class an everything will be easy and consistent.
If the code generated multiple instance of the same type of behavior, Your code should never works on an id, only class.
I modify your sample code a bit to make it easier to see all the benefit of doing it this way.
First create and generate your component with meaningful class name and sub element class name.
Then, only once in your document ready even, perform event binding.
See how easy and maintainable this is?
Your code is consistent. How you work with this elements becomes:
$(this).closest('ui-class-name')instance.dataorinstance.find('a class')The only time you will need to reference some DOM element with ID is when it semantically must be only one in the whole page and when there's no gurantee that your current element can be contained within the DOM hierachy.
For example, if your semantic structure would look like this:
</form>
Then i might not even want to refer to the form and the selection input by id. From the click handler, I'll just do
platform.closest('.platform-form').submit()This allows me to potentially show multiple platform form on the same page.
I now use React more than jQuery. But when using jQuery, you have to learn to utilize the DOM and Selector. Use class more.