Hakan Hakan - 1 year ago 106
Javascript Question

Get index/position of element in div - pure javascript

How to get the position of last child in a element, I mean the number (the index)?

I want the code below to alert "4", since there is four elements.

<!DOCTYPE html>
<title>Untitled Document</title>
<div id="container">
<div id="div1">div1</div>
<div id="div2">div2
<div id="child_div1">div2</div>
<div id="child_div2">div2</div>
<div id="div3">div3</div>
<div id="div4">div4</div>

var container = document.getElementById('container'),
last_child_of_container = container.childNodes.length;

alert('position of last div is'+ last_child_of_container);


Answer Source




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download