2017-03-08 9 views
0

私がやろうとしていることは、jsの作成されたテキストノードにボタンを作成することです。その後、spmetの値を見つけ、データベースから質問(spmet)を削除します。Firebaseは、子の値に基づいて親のキーを見つけること

しかし、正しく参照する方法を見つけられず、削除したい特定の値を見つけることができません。あなたは各質問のためのHTML要素を生成する場合

this is the the way to remove questions

This is the firebase layout

var btn = document.createElement("BUTTON"); 
var btnText = document.createTextNode("x"); //create button 

btn.appendChild(btnText); 
tekst.appendChild(btn); 
btn.id = "questionBtn"; 

//bytter enter som gir linjeskift til <br> 
tekst.innerHTML = tekst.innerHTML.replace(/\n/g, '<br>'); 

chat.appendChild(bubble); 

setTimeout(function(){ 
    chat.classList.add('visible') 
}, 1); 

chat.scrollTop = chat.scrollHeight; 
console.log(bubble); 
// Function to remove the question on the button generated 
tekst.onclick = function removeQ(){ 

    window.alert("Knapp funker"); 

    var ref = database.ref(); 
    ref.child('spm') 
     .orderByChild('spmet') 
     .equalTo(spmet) 
     .once('value', function(snap) { 
     //remove the specific spmet parent 
     window.alert(snap.val()); 

    }); 
    document.getElementById("cont1").removeChild(bubble); // removes text from page 
    var spmRef = ?? 
    spmRef.remove(); //can't reference properly 
} 

答えて

0

、そのIDを保つ(私は「x」を押したときに、他の画像は、データベースからその質問を削除します)そのHTML要素の属性として質問してください:

ref.child("spm").on("child_added", function(snapshot) { 
    var div = document.createElement("div"); 
    div.id = snapshot.key; 
    div.innerText = snapshot.child("spmet").val(); 
    div.onclick = onSpmClick; 
    questionContainer.appendChild(div); 
}); 

ここで、ユーザーが質問の1つをクリックすると、そのキーを取得できます除外して除外する:

function onSpmClick(e) { 
    var key = e.target.id; 
    ref.child("spm").child(key).remove(); 
} 
関連する問題