0

I have this code

<div class="g-popupgrid-item g-zoom" data-size="395x385">

I want to populate the 395x385 dynamically with the image size.

I have a JS function which gets the image size, I don't know how to print/echo/document.write it to be within the HTML tag.

2
  • 4
    you won't print/echo/document.write. you use DOM and .setAttribute() in jquery. it's as simple as $('.g-popupgrid-item .g-zoom').attr('data-size', var_with_value_you_want) Commented Jul 26, 2016 at 19:22
  • 1
    And of course, you can do this without jQuery: document.querySelector("g-popupgrid-item g-zoom").setAttribute("data-size", your_value_here); Commented Jul 26, 2016 at 19:26

2 Answers 2

2

I would avoid using jQuery for such a trivial task unless you are already using it in your project. Plain javascript:

var div = document.querySelector('.g-popupgrid-item');
var newSize = '100x100'; // for example
div.setAttribue('data-size', newSize);
Sign up to request clarification or add additional context in comments.

1 Comment

or using dataset in compatible browsers div.dataset.size = newSize
2

Get the Node using JavaScript, then modify it to fit your needs:

var container = document.querySelector(".g-popupgrid-item .g-zoom")
container.setAttribute("data-size", value)

Keep in mind this will only affect the first element with those classes, if you want affect them all, use the code below:

var containers = document.querySelectorAll(".g-popupgrid-item .g-zoom")
containers.forEach(function(container) {
  container.setAttribute("data-size", value)
})

Also keep in mind that containers is not an Array it's a NodeList. Read more here

EDIT: According to @Eoin, the .forEach() method on NodeLists is not supported in Firefox. You can also use a standard for loop in place of it:

var containers = document.querySelectorAll(".g-popupgrid-item .g-zoom")
for (var i = 0; i < containers.length; i++) {
  containers[i].setAttribute("data-size", value)
}

7 Comments

You point out a nice feature of returning a NodeList rather than the HTMLCollection that getElementsByClassName produces - the forEach method. You don't need to recast as an array.
@Damon I just read a bunch about the differences between NodeList and HTMLCollection, because it was one of the things that I really didn't understand about browser js. Good timing on @Eoin 's part, though
I have tried to implement it but now I'm getting an error TypeError: containers.forEach is not a function here's my full code
<script> var images = document.getElementsByClassName("image"); for (var i = 0; i < images.length; ++i) { images[i].addEventListener("load", (event) => { var image = event.target; image.dataset.size = image.naturalWidth + "x" + image.naturalHeight; console.log(image.dataset.size); });//works at this point, then I add the next part var containers = document.querySelectorAll(".g-popupgrid-item .g-zoom"); containers.forEach(function(container) { container.setAttribute("data-size", image.dataset.size); }); } </script>
@Eoin it's an issue of browser compatability. You can also use a standard for loop, which will work in Firefox
|

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.