Find Element by ID with number in the ID

Find Element by ID with number in the ID

by Nirvanachain

HTML

<div id="my-div-1">Title 1</div>
<div id="my-div-2">Title 2</div>
<div id="my-div-3">Title 3</div>
<div id="my-div-4">Title 4</div>

JavaScript

var myNum = 3;
var el = document.getElementById('my-div-' + myNum);
if (el == undefined) {
    alert("Fail");
} else {
    alert("Success!!! " + el.innerHTML);
}