JS multitype multi type array javascript

     <div id="idMyDiv"></div>
    <div id="idOtherDiv"></div>
 
    <script>
    var myDiv = document.getElementById('idMyDiv');
    var otherDiv = document.getElementById('idOtherDiv');
    // array can contains string, number, object etc.
    var arr = new Array('5', 3, 'hello', 12, myDiv);
 
    if (contains(arr, myDiv)) {
        alert('Contains myDiv');
        idMyDiv.innerHTML = "I found You!!!!! :)";
    } else {
        alert('Does not contains myDiv');
    }
 
    if (contains(arr, otherDiv)) {
        alert('Contains otherDiv');
    } else {
        alert('Does not contains otherDiv');
    }
 
 
    function contains(arr, valueOrObject) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] === valueOrObject) {
                return true;
            }
        }
        return false;
    }
 
    </script>
This entry was posted in Javascript css html and tagged , . Bookmark the permalink.


Comments are closed.

549 views