Object is coming null

Asked

Viewed 44 times

0

I have the following code, but the var ob this coming null.

<head>
<script>
   function pisca(item) {
       var ob = document.getElementById(item);

       if (ob.style.color=="red"){
          ob.style.color="black";
       }else{
          ob.style.color="red";
       }
   } 
</script>

</head>

<body>

<div id="piscar" style="color:#F00">
   Texto piscando
</div>

 <script>
    t = setInterval("pisca('piscar')",500);
 </script>

 </body>
  • Good morning. You checked this on the console, which is coming null?

  • "var ob this coming null." It wouldn’t be before undefined ? In what situations is this happening ? Is there any way to show it to us ? Where is this null ?

  • It is worth remembering that the var ob has value only within the function pisca. If you want it to have global scope, you’d have to do it outside of function var ob; and within the function without the var, only ob = document.getElementById(item);.

  • Thanks for the feedback, worked out just that!!

2 answers

0

I tried to simulate your problem here, but the same did not occur.:

function pisca(item) {
  var ob = document.getElementById(item);
  if (ob.style.color == "red") {
    ob.style.color = "black";
  } else {
    ob.style.color = "red";
  }
}

t = setInterval("pisca('piscar')", 500);
<div id="piscar" style="color:#F00">
  Texto piscando
</div>

although I would change the way to call the flashing function in the setInterval, after all this form is not recommended (READ MORE)

function pisca(item) {
  var ob = document.getElementById(item);
  if (ob.style.color == "red") {
    ob.style.color = "black";
  } else {
    ob.style.color = "red";
  }
}

t = setInterval(function () {
  pisca('piscar')
}, 500);
<div id="piscar" style="color:#F00">
  Texto piscando
</div>

  • Thanks for the answer, I forgot to mention that I am including this code in jsp, and in this jsp I am using the <h:form that you are doing for some reason that this script is null object. I don’t know if I could explain, but when I take the <h: form it works and I have several scripts and they are working normally only the one that is giving problem.

  • then your problem is not with the JavaScript, but with the jsp (which completely escapes my domain), I advise you to analyze the final HTML (right in the browser) and see if it is as expected.

0

As the variable ob was declared with var within the function pisca(), it only has scope within the function.

You can solve it by having it have global scope by removing the var:

function pisca(item) {
    ob = document.getElementById(item);

    if (ob.style.color=="red"){
       ob.style.color="black";
    }else{
       ob.style.color="red";
    }
} 

After the execution of the function, the variable ob becomes a value global.

Example:

function pisca(item) {
    ob = document.getElementById(item);

    if (ob.style.color=="red"){
       ob.style.color="black";
    }else{
       ob.style.color="red";
    }
}

function teste(){
   console.log(ob);
}

pisca('piscar');
<div id="piscar" style="color:#F00">
   Texto piscando
</div>
<br>
<input type="button" onclick="teste()" value="Verificar valor de ob">

  • Thank you very much =).

  • And then, buddy, it worked?

  • Opa gave yes mtu thanks =)

Browser other questions tagged

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