Ask how to get the index of td in tr in native js? Something like the index () method in jQuery?


<html>
<body>
<table>
<tr>
<td id="a">1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
<body>
<script src="http://172.29.5.201/assets/js/core/jquery.min.js"></script>
<script>
alert($("-sharpa").index());</script>
</html>
Mar.25,2021

function searchIndex(id){
    // td
    var td = document.getElementById("td");
    // td
    var arr = Array.from(td.parentNode.children);

    //
    arr = arr.filter(function(item){
        return td == item;
    });
}

function getIndexFromDomOrSelector(domOrSelector){
    let dom;
    // domSelector
    if(domOrSelector instanceof HTMLElement || domOrSelector instanceof Element){
        dom = domOrSelector;
    }else if(typeof domOrSelector === "string"){
        dom = document.querySelector(domOrSelector);
        if(!dom) throw new Error("selectordom")
    }else{
        throw new Error("");
    }
    // 
    const brothers = Array.from(dom.parentNode.children);

    return brothers.indexOf(dom);
}

// 1
getIndexFromDomOrSelector("-sharpa")
// 2
const td = document.getElementById("a");
getIndexFromDomOrSelector(td)

document.getElementById('a').cellIndex
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3d7cd-2b6fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3d7cd-2b6fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?