Add a dynamic ruby generated id to a Javascript function -
i have script on html.erb file.
to cut long story short have line in script
$('img:gt(0)').hide();
i want add dynamic id img selector. dynamic id is
<%= hrct.id %>
i've tried
var imgvar = <%= hrct.id %>; $('#img_+ imgvar img:gt(0)').hide();
no good.
and
var imgvar = <%= hrct.id %>; $('#img_+ (imgvar) img:gt(0)').hide();
no good.
help?
edit: have tried
$(function(){ var imgvar = <%= hrct.id %>; $('#img_'+ imgvar +' img:gt(0)').hide(); // hide first image when page loads var img = $('#<%= hrct.id %>').children(), l = img.length; $('#up, #down').on('click', function() { var = img.index( img.filter(':visible').hide() ); var t = this.id === 'up' ? ++i : --i; img.eq( t === l ? 0 : t ).show(); }); });
i have included full function. if @ developer tools get
instead of
$('#img_3 img:gt(0)').hide();
in fact,
$('#img_<%= hrct.id %> img:gt(0)').hide();
gives
$('#img_3 img:gt(0)').hide();
but function doesn't work should
try this
var imgvar = <%= "#img_#{hrct.id}" %>; $(imgvar +' img:gt(0)').hide();
i hope helpfulf
Comments
Post a Comment