Error: Cannot read Property 'style' of null

Asked

Viewed 3,922 times

1

I am doing a work in html, css, javascript and ajax and the goal is to change the style of the tab with the sensor name id when it is clicked, but it says that the element does not exist and I wanted to know what mistake I am making.

Basically I am importing from an xml file and placing tabs with the sensor id of the xml file inside a tag with the name list_sensores

        strHtmlNomes = "";
        for(i = 0; i < nomesSensores.length; i++)
        {
            nomeSensor = nomesSensores[i];
            strHtmlNomes +='<div><a href="#" id="'+nomeSensor+'" class="tablinks" onclick="openSensor('+nomeSensor+')">'+nomeSensor+'</a></div>';       
        }



        document.getElementById("list_sensores").innerHTML = strHtmlNomes;

and before closing html I have this javascript script

function openSensor(nomeSensor){
    document.getElementById(nomeSensor).style.color = "blue";
}

inserir a descrição da imagem aqui

1 answer

1


  • the variableName is never numerical

  • Does the name repeat in some cases, or are they just unique names? Spaces are used in the name assigned to the id?

  • the name is always unico <sensor id="1"> <name>Temperature</name> <Description>Temperature sensor</Description> </sensor> <sensor id="2"> <name>Quality_do_ar</name> <Description>Air quality sensor</Description> </sensor> </sensor> <sensor id="3"> <name>Fluxo_de_transito</name> <Description>Traffic flow sensor</Description> </sensor> <sensor id="4"> <name>Activities_cardiaca</name> <Description>Heart activity sensor installed on people</Description> </sensor>

  • html id is with sensor name not with number, sensor list is xml file

  • Maybe it’s your parameter onclick, as you are passing a string as argument to its function, try putting it in HTML like this: onclick="openSensor(\'' + nomeSensor + '\')">'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.