Getting text from an element

Asked

Viewed 47 times

-1

I’m trying to do a basic validation in pure javascript.

works as follows; I have a <h1> where the user profile is informed and I need to check the text, as an example:

<h1 class="perfil-usuario">SISTEMA<h1>

And I also have the condition:

 <script>

  var perfilSistema = document.querySelector(".perfil-sistema");

  if (perfilSistema  == 'SISTEMA' ) {
    window.location = "index.html";
  }
 </script> 

But it is not directing the user to the index when it receives SYSTEM. Grateful to those who help.

3 answers

0

Paste these lines into your code and test

With pure Javascrip

  var cel = document.querySelector('.perfil-usuario').textContent; 
 if(cel == "SISTEMA"){
 alert("USER ENCONTRADO");
 window.open('http://www.google.com', '_self');
 //ou use  window.location = 'https://www.google.com' 
 }
 if(cel!="SISTEMA"){
 alert("USER NÃO ENCONTRADO");

 }

With jJquery

var cel = $('.perfil-usuario').text(); 
     if(cel == "SISTEMA"){
     alert("USER ENCONTRADO");
     open('http://www.google.com', '_self');
     }
     if(cel!="SISTEMA"){
     alert("USER NÃO ENCONTRADO");

     }

0

There are two problems with your code. The first is that the query for this class will return null since you are looking for .perfil-sistema when the class name in your html is .perfil-usuario.

Also, you are comparing an object with a string. You should compare the "content" (in this case, the "text") of the element with Node#textContent or Element#innerHTML():

var perfilSistema = document.querySelector(".perfil-usuario");

if (perfilSistema.textContent == 'SISTEMA') {
  window.location = "index.html";
}
<h1 class="perfil-usuario">SISTEMA<h1>

  • It even worked @Renan, but the class has no mandatory standard no. You’re kind of wrong about that

  • @Francisvagnerdaluz did not say that it is mandatory, I said that your consultation is wrong.

  • My consultation?

  • @Francisvagnerdaluz <h1 class="perfil-usuario">...<h1> document.querySelector(".perfil-sistema").

  • Now that I understood.. It was just an example, just as it was written there in the question. Examples are not real. Where I am programming is right

  • @Renan probably by SISTEMA<h1> be closed incorrectly, should be </h1>, the textContent may be bringing something else, even spaces or break lines.

  • 1

    @Francisvagnerdaluz On the name of the classes according to the question presented, it must contain the real code of the problem, to avoid that the answers are taken on unreal problems.

Show 2 more comments

0


First have an error in your code, one should close the H1 tag so: </h1>

  var perfilSistema = document.getElementById("psistema");
  var text = perfilSistema.innerHTML;
  alert(text);
  
  if (text  == 'SISTEMA' ) {
   alert('estou dentro do if, vou redirecionar'); 
   window.location = "index.html";
  }
<h1 class="perfil-usuario" id='psistema'>SISTEMA</h1>

Explanation of the code:

This is to get the html from inside H1:

var text = perfilSistema.innerHTML;

and this was used to catch the element with the id pSistema

var perfilSistema = document.getElementById("psistema");

Browser other questions tagged

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