0

$(window).load(function() {
  $("img").each(function() {
    $(this).after("<div id='watermark'>Hello</div>");
    var width = $('#watermark').eq(0).width();
    //This outputs 0s
    console.log(width);
    $("#watermark").css("margin-left", "-" + width.toString() + "px");
  });


  $("img").click(function() {
    setTimeout(function() {
      $("img").each(function() {
        //this outputs 37
        console.log($('#watermark').eq(0).width());
        $(this).after("<div id='watermark'>Hello</div>");
      });
    }, 500);
  });

});

I am trying to get the width of the watermark I insert over images so that I can center the watermark by subtracting half the width from the left margin. However, for some reason, I am getting 0s from the .width() function even though I know that the div exists because I had already inserted the div in the previous line. Also, in the second part of the code, you'll see that when the image is clicked, the width is returned properly. Why am I getting 0s in the first half?

Edit: Here is the CSS on Watermark:

#watermark {
  opacity: 0.5;
  z-index: 99;
  position: absolute;
  color: white;
  top: 50%;
  left: 50%;
  text-align: center;
}

Is position: absolute causing a problem?

6
  • Is the div visible? Commented Jun 4, 2016 at 22:11
  • 1
    Can't duplicate ID's in page...they are unique by definition. Need to see css used on watermark. Suspect it is position:absolute and you haven't used adequate css and the text is overflowing the element Commented Jun 4, 2016 at 22:12
  • Create a demo in jsfiddle.net Commented Jun 4, 2016 at 22:13
  • It appears to be "working" for me - in that I can see a number greater than zero output to the console: jsbin.com/yumozojiqo Ditto what @charlietfl says about duplicate IDs. Hard to say without seeing the markup you are using. Commented Jun 4, 2016 at 22:15
  • 1
    @MikeMoore but watermark would usually layer over image which is why I suspect position:absolute without enough properties to give it any width Commented Jun 4, 2016 at 22:17

2 Answers 2

2

Based on css update an absolute position element has no width unless you either declare width or set both left and right.

In your css rule try including either width:100% or right:0 and then run your code and you will have some width value

Sign up to request clarification or add additional context in comments.

1 Comment

Important to understand that what you visibly see is a zero width container with it's content overflowing it
1

I have added the code here in a snippet, and it seems to work, both initially and in the img click handler. Here is the snippet:

$(window).load(function() {
  $("img").each(function() {
    $(this).after("<div id='watermark'>Hello</div>");
    var width = $('#watermark').eq(0).width();
    console.log("Initial width: ", width);
//        $("#watermark").css("margin-left", "-" + width + "px");
  });

  $("img").click(function() {
    setTimeout(function() {
      $("img").each(function() {
        console.log("Handler width: ", $('#watermark').eq(0).width());
        $(this).after("<div id='watermark'>Hello</div>");
      });
    }, 500);
  });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
#watermark {
  opacity: 0.5;
  z-index: 99;
  position: absolute;
  color: white;
  width: 100%;
  top: 50%;
  left: 50%;
  text-align: center;
}
</style>
<img src="http://www.va.gov/OSDBU/images/business.png">

Now, I had to add some HTML to work with, since none was provided. From what I could discern from the Javascript code, this was all that was needed. If there's something missing, please show the details (e.g. the actual code).

1 Comment

This is pretty much what I was going to say

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.