<div id="file2"></div> <div id="file3"></div> <div id="file1"></div> <script> alert(document.querySelector('[id^="file"]').id); </script>