简体   繁体   中英

Uncaught TypeError: Cannot set property 'value' of null even though it is not null

I am trying to delete anything that might be on a text field. These text fields have different id's and the id needs getting generated from a count. So i know that the id has been created and it exsists. Here is my js code.

    var score= 2;
    function hideNewDiv(){

        document.getElementById('showAnotherDiv').style.display='inline';
        score--;
        document.getElementById("Feedback_"+score+"_score").value = "";
        document.getElementById('newSF' + score).style.display='none';

        if (score== 2){
            document.getElementById('hideAnotherDiv').style.display='none';
        }
    }

when it gets to setting the value to "" i get the error. Any help would be greatly appreciated

thanks in advance.

This implies there is no element with ID "Feedback_"+score+"_score" . score starts at 2, but you decrement it each time you run your function, so it becomes 1 , then 0 , then -1 , and so on, depending on how many times you call it. You need to validate the value of score before trying to access an element using it.

I was able to figure it out. My problem was not with Javascript, the problem was behind my HTML. I had not specified an id. i forgot i only had a name specified. common rookie mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM