jQuery css() not working in if statement -
i have if statement checks see if image displayed in dom smaller 600px wide, , if was, image resized different width. it's not working. correct value displayed in alert doesn't passed css line below. idea why happening?
jquery
$('span.i_contact').each(function() { var imgwidth = parseint($(this).data('image-width')); console.log(imgwidth) if (imgwidth < 600) { var newwidth = ((imgwidth / 600) * 300) alert(newwidth) $(this).css({ "width":newwidth }) } var pos_width = ($(this).data('poswidth')) / 2.025; var pos_height = ($(this).data('posheight')) / 2.025; var xpos = ($(this).data('posx')) / 2.025; var ypos = ($(this).data('posy')) / 2.025; var taggednode = $('<div class="tagged" />') taggednode.css({ "border":"5px solid orange", "width":pos_width, "height":pos_height, "left":xpos, "top":ypos }); var n = $(this).data('index'); $('.i_tagmap' + n).append(taggednode); }); $("span.o_contact").each(function() { var imgwidth = parseint($(this).data('image-width')); console.log(imgwidth) if (imgwidth < 600) { var newwidth = ((imgwidth / 600) * 300); alert(newwidth) $(this).css({ "width":newwidth }); } var pos_width = ($(this).data('poswidth')) / 2.025; var pos_height = ($(this).data('posheight')) / 2.025; var xpos = ($(this).data('posx')) / 2.025; var ypos = ($(this).data('posy')) / 2.025; var taggednode = $('<div class="tagged" />') taggednode.css({ "border":"5px solid green", "width":pos_width, "height":pos_height, "left":xpos, "top":ypos }); var n = $(this).data('index'); $(this).append(taggednode); });
erb (how images generated)
<% n = steps.index(step) %> <h2 style="margin-left:20px;"> step <%= n + 1%></h2> <div class="stepcontainer"> <div class="steptext"> <%= step.instruction %> </div> <div class="modalbutton"> <%= render(step.flags.new) %> </div> <% if step.input_contact.present? %> <div class="productimg"> <div class="image_panel<%= n %>" style="float:left; width:600px; position:relative;"> <span class="i_contact i_contact<%= n %>" data-pos-x="<%= step.i_connection.pos_x %>" data-pos-y="<%= step.i_connection.pos_y %>" data-pos-width="<%= step.i_connection.pos_width %>" data-pos-height="<%= step.i_connection.pos_height %>" id="spanid<%= n %>" data-image-width="<%= step.i_connection.image.dimensions.first %>" data-index="<%= n %>"></span> <%= link_to image_tag(step.i_connection.image.image.url(:medium), id: "iconnection#{n}" ), "#{step.i_connection.image.image.url(:large)}", class: "fancybox" %> <div class="i_tagmap<%= n %>"></div> </div> </div> <% if step.i_connection.cord? && !step.o_connection.dongle? %> <div class="cableimg"> <%= image_tag(step.i_connection.cord_type.image.url(:thumb), :class => "orange") %> </div> <% end %> <% end %> <!-- end of step.input_contact.present --> <% if step.o_connection.cord? && !step.o_connection.dongle? %> <div class="cableimg"> <%= image_tag(step.o_connection.cord_type.image.url(:thumb), :class => "green") %> </div> <% end %> <div class="productimg"> <div class="image_panel<%= n %>" style="float:left; width:600px; position:relative;"> <span class="o_contact o_contact<%= n %>" data-pos-x="<%= step.o_connection.pos_x %>" data-pos-y="<%= step.o_connection.pos_y %>" data-pos-width="<%= step.o_connection.pos_width %>" data-pos-height="<%= step.o_connection.pos_height %>" id="spanid<%= n %>" data-image-width="<%= step.o_connection.image.dimensions.first %>" data-index="<%= n %>"> </span> <%= link_to image_tag(step.o_connection.image.image.url(:medium), id: "oconnection#{n}"), "#{step.o_connection.image.image.url(:large)}", class: "fancybox" %> <div class="o_tagmap<%= n %>"></div> </div> </div> </div>
in jquery, .each referring span, therefore inside of .each
need traverse dom access image.
since appears image immediate sibling after span, .next
seems appropriate (although there many other methods used):
$('span.i_contact').each(function() { var imgwidth = parseint($(this).data('image-width')); console.log(imgwidth); if (imgwidth < 600) { var newwidth = ((imgwidth / 600) * 300) alert(newwidth); $(this).next().css({ "width":newwidth }) } ...
Comments
Post a Comment