jQuery won't act on a partial rendered through AJAX in Rails -
my app has called "memos". , can reply other memos using @ tags inside memo. example:
hi reply @123
then, if click, @123, memo #123 rendered underneath memo you're viewing.
_memo.html.erb:
<div class="memo"> <div class="memo-content"> <%= raw simple_format(memo.content).gsub(/@([a-z0-9a-z]+)/, '<a data-remote="true" class="marker" href="/memos/\1">@\1</a>')%> </div> </div>
app/assets/javascripts/memo.js:
$(function() { $(".marker").on("ajax:success", function(e, content){ console.log("hi") $(".memos-container").append(content); }); })
memos_controller.rb:
def show @memo = memo.find(params[:id]) if request.xhr? render partial: "shared/memo", locals: { memo: @memo } end end
the first click works fine. if click on @123
, memo #123 rendered in page. if click on other @ tags inside first memo, work too.
problem is, nothing inside rendered memo clickable. if memo #123 has @456 tag, , click on it, nothing rendered.
looking @ network console in chrome, is making get
request, jquery not acting on newly rendered dom elements.
i've looked @ similar questions on so, , use .on
, which, far can tell i'm doing. i'm total javascript newb.
i got work using this:
$(function() { $(document).ajaxsuccess(function(e, content){ console.log(content.responsetext) $(".memos-container").append(content.responsetext); }); })
but if there's better way i'm open suggestions :)
now turbolinks making act wonky. that's different topic...
Comments
Post a Comment