Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending the elements within div using appendchild attribute

I have a div as :

<div id="div1" width="100px" height="100px">

</div>

Now within this div I want to place 20 elements, but dynamically it can grow upto 50 elements(images) also.

I am using the following code to append these elements in a div,

var i = document.createElement("img");
var d= document.getElementById("div1");
d.appendchild(i);

Now, the issue is ,as the number of elements increase the elements are going out of div ,and if I use the max-width and max-height on images, the result doesnt change:

i.setAttribute('max-width', '100%');
i.setAttribute('max-height', '100%');

Is there anything which I am missing?

Edit: The images need to shrink as the div size is fixed

like image 486
user1907849 Avatar asked May 19 '15 07:05

user1907849


People also ask

What is append and appendChild?

Element.append() allows you to also append string objects, whereas Node.appendChild() only accepts Node objects. Element.append() has no return value, whereas Node.appendChild() returns the appended Node object.

Can you append to a div?

HTML code can be appended to a div using the insertAdjacentHTML() method. However, you need to select an element inside the div to add the code.

Why we use append appendChild in JavaScript?

The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node. If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position.

Can you appendChild multiple elements?

The append method allows you to append multiple elements or text nodes to a parent node. As you can see, you can supply an unlimited amount of arguments to the append() method and it will append everything to the parent.


3 Answers

If the width is fixed and the height is dynamic. The image will shrink and they will get stacked. Check my fiddle here

img {
    width:100%;
    display:inline-block;
}

<div style="width:100px;border: 1px solid black;">

    <img src="http://www.brightlinkprep.com/wp-content/uploads/2014/04/sample.jpg" />
    <img src="http://www.brightlinkprep.com/wp-content/uploads/2014/04/sample.jpg" />
    <img src="http://www.brightlinkprep.com/wp-content/uploads/2014/04/sample.jpg" />

</div>
like image 199
Drixson Oseña Avatar answered Sep 27 '22 20:09

Drixson Oseña


Cant think of a nice way to do it with percentages,

var imags = document.getElementsByTagName('img');
var count = imags.length;
for (var index= 0,l= count;index<l;index++){
    imags[index].setAttribute('height', (100/count)+'%');;
}

its not pretty but should work.

like image 42
znap026 Avatar answered Sep 27 '22 20:09

znap026


i smushed something together from all the answers that fits your needs (if i understand you correctly)

https://jsfiddle.net/b30d88g6/3/

the div has fixed width/height and the images wont get out of the div

function add_img() {

    var i = document.createElement("img");
    i.src= "https://jsfiddle.net/img/logo.png";
    var d= document.getElementById("div1");
    d.appendChild(i);

    var imags = document.getElementsByTagName('img');
    var count = imags.length;
    for (var index=0, l=count;index<l;index++){
        imags[index].setAttribute('height', (100/count)+'%');;
    }
}
like image 22
roeb Avatar answered Sep 27 '22 20:09

roeb