Javascriptda ota elementni tanlashda muommo

0 ovoz
46 marta ko‘rilgan
so‘radi 31 avgust parmonov98 (850 bal)
tahrirlandi 02 sentabr Saidolim
function getComments(){
console.log(123);
const ajax = new XMLHttpRequest();
let postID = document.getElementById('post_id').value;
ajax.onreadystatechange = function () {
switch(ajax.readyState){
case 0:
console.log('0.request not initialized!');
break;
case 1:
console.log('1.connection established!');
break;
case 2:
console.log('2.request received !');
break;
case 3:
console.log('3.processing request!');
break;
case 4:
console.log('4.request finished and response is ready!');
if(ajax.status == 200){
console.log('Everything is OK!');
let res = document.getElementById('comment-body');
res.innerHTML = ajax.responseText;
}else{
console.log('There is an error. it seems the requested file does not exist!');
}
break;
}
}
let url = 'http://salomdunyo.gr/get.php?query='+postID;
ajax.open('GET', url, true);
ajax.send();
}

yuqoridagi funksiya oyna to'liq yuklanib bo'lishi bilan ishga tushib kerakli kommentlarn html sahifaga chiqaradi.

<div class="flex flex-around add-comment">
<div class="comment">
<textarea name='com_text' id="comment-textarea" class="comment-textarea" rows="5" cols = "20"placeholder="<?=$this->data['post']['COM_YOUR_COM']?>"></textarea>
<input type='text' name='parent_id' id='parent_id' value='0' hidden />
<input type='text' name='post_id' id='post_id' value="<?=$post['id']?>" hidden />
<input type='text' name='post_author' id='post_author' value="<?=$post['post_author']?>" hidden />
</div>
<div class='flex column comment-buttons'>
<button type="submit" name='addcom' id='addcom' onclick="comment_request('add_comment');" class="comment-add-button"><?=$this->data['post']['COM_BUTTON']?></button>
</div>
</div>

bu esa komment joylashtirish uchun html tags

function comment_request(typeAction, id){
console.log(typeAction);
const ajax = new XMLHttpRequest();
let input = document.getElementById('search_input').value;
let url = '';
  ajax.onreadystatechange = function () {
switch(ajax.readyState){
case 0:
console.log('0.request not initialized!');
break;
case 1:
console.log('1.connection established!');
break;
case 2:
console.log('2.request received !');
break;
case 3:
console.log('3.processing request!');
break;
case 4:
console.log('4.request finished and response is ready!');
if(ajax.status == 200){
switch(typeAction){
    case 'add_comment':
            id = '';
            let input = document.getElementById('comment-textarea').value;
            let postID = document.getElementById('post_id').value;
            let author = document.getElementById('post_author').value;
            let parentID = document.getElementById('parent_id').value;
            let res = document.getElementById('comment-item').parentNode;
        console.log(res);
            let newItem = document.createElement("DIV");
            newItem.setAttribute("class", "comment-item flex flex-between top");
            newItem.setAttribute("id", "comment-item");
            newItem.innerHTML = ajax.responseText;
            var sp2 = document.getElementById("comment-item");
            res.insertBefore(newItem, sp2);
            url = 'http://salomdunyo.gr/add.php?query='+input+'&author='+author+'&postID='+postID+'&parent_id='+parentID;
        break;
    case 'edit_comment':
        break;
    case 'delete_comment':
        break;
}
}else{
console.log('There is an error. it seems the requested file does not exist!');
}
break;
}
}
ajax.open('GET', url, true);
ajax.send();
}    

 

belgilangan qatorga nisbatan xatolik chiqarmoqda.

xatolik quyidagicha:

 Uncaught TypeError: Cannot read property 'parentNode' of null
    at XMLHttpRequest.ajax.onreadystatechange (sahifa:353)

1 ta javob

0 ovoz
javob berdi 16 sentabr AlisherN (19 bal)
case 'add_comment':
            id = '';
            let input = document.getElementById('comment-textarea').value;
            let postID = document.getElementById('post_id').value;
            let author = document.getElementById('post_author').value;
            let parentID = document.getElementById('parent_id').value;
            let res = document.getElementById('comment-item').parentNode;
        console.log(res);
            let newItem = document.createElement("DIV");
            newItem.setAttribute("class", "comment-item flex flex-between top");
            newItem.setAttribute("id", "comment-item");
            newItem.innerHTML = ajax.responseText;
            var sp2 = document.getElementById("comment-item");
            res.insertBefore(newItem, sp2);
            url = 'http://salomdunyo.gr/add.php?query='+input+'&author='+author+'&postID='+postID+'&parent_id='+parentID;
        break;

Kodizni belgilangan qismiga qarab ko'ring. Adashmasam 1-sida berilgan id ni topomayapti. Ya'ni siz 'comment-item' ni qo'shishdan oldin qidiryapsiz

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...