0

I am trying to find the width of a child element. My template is in rails:

 <div id="wrapper">
        <div id = "inner"></div>
        <%= image_tag(photo.file_name) %>
 </div>

I want to get the width of the image after "inner" using javascript. Any ideas on how I can go about this?

user1054740
  • 503
  • 2
  • 7
  • 13

2 Answers2

1

if you want to keep it straight Javascript without additional libs

img =document.getElementById('wrapper').getElementsByTagName('img');
 for(i=0;i<img.length;i++){
   console.log(img[i].clientWidth)
 }
Lewis E
  • 327
  • 1
  • 7
0

If the image is the only thing in the "wrapper" div other than the "inner" div, something like this will work https://stackoverflow.com/a/842346/1960453

Either use jQuery, or altCognito's function to get the width of the sibling of the "inner" div

Community
  • 1
  • 1
David T
  • 81
  • 7