scythercas
@scythercas

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

innerTextが特定の文字列のcellのidを取得したい

image.png

innerText""が格納されているcellのidを取得したいです。
表は下の関数で作っています。どなたかやり方を教えてください。

function init(){
        var num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,""];
        var m = num.length;
        while(m){
            var l = Math.floor(Math.random() * m--);
            var t = num[m];
            num[m] = num[l];
            num[l] = t;
        }
        var b = document.getElementById("board");
        for(var i=0;i<4;i++){
            var tr = document.createElement("tr");
            for(var j=0;j<4;j++){
                var td = document.createElement("td");
                td.className = "cell";
                td.id = "cell" + i + j;
                td.innerText = num[4*i+j];
                tr.appendChild(td);
            }
            b.appendChild(tr);
        }
    }
0

2Answer

タイトル(特定の文字列)に対する回答であれば

Array.from(document.querySelectorAll('td')).filter(td => td.innerText === '特定の文字列')[0].id

でしょうか。空文字であれば

document.querySelector('td:empty').id

で構いません。

3Like

Comments

  1. @scythercas

    Questioner

    分かりづらい質問文でした、すみません!
    後者の空文字のことを指しています。
    ありがとうございます!

Comments

  1. @scythercas

    Questioner

    ありがとうございます!!
    助かります🙇‍♂️

Your answer might help someone💌