0
I was with a problem regarding how to pass the data to the front and change the visualization of such information, after giving a search, I gathered some things I was researching, it was very simple, but it worked at first (I was wrong in this part, I did not test with more content).
The code, takes back values and only displays true or null (displays nothing rs):
{{#each qrs}}
<h2 class="d-inline" id="horarioDaAula1">{{horario1}}</h2>
<h2 class="d-inline" id="horarioDaAula2">{{horario2}}</h2>
<h2 class="d-inline" id="horarioDaAula3">{{horario3}}</h2>
<h2 class="d-inline" id="horarioDaAula4">{{horario4}}</h2>
<h2 class="d-inline" id="horarioDaAula5">{{horario5}}</h2>
<br>
{{/each}}
The script I used to change the view, to look more beautiful in a certain way.
<script>
// Pegar o valor e exibir ícones. Para colocar na visualização da presença do aluno
for(i = 1; i<=5; i++){
var n = "#horarioDaAula";
var horario = n + i;
var myElement = $(horario);
var myText = myElement.text();
if(myText == "true"){
myElement.html('<i class="fas fa-check-circle fa-xs" style="color: green"></i>');
}else{
myElement.html('<i class="fas fa-times-circle fa-xs" style="color: red"></i>');
}
}
</script>
Until then everything was quiet, but when {{#each qrs}} was implemented, more values appear according to each Collection. With that it was like the following image:
I’m not very knowledgeable, I’m new, so I figured (I think) jquery only takes one ID and not all H2 that have the same ID.
How could I be settling this question, or doing otherwise?
EDIT 1: How it appears on the front with the use of {{#each qrs}}
<h2 class="aa d-inline" id="horarioDaAula1">true</h2>
<h2 class="aa d-inline" id="horarioDaAula2">true</h2>
<h2 class="aa d-inline" id="horarioDaAula3">true</h2>
<h2 class="aa d-inline" id="horarioDaAula4">true</h2>
<h2 class="aa d-inline" id="horarioDaAula5">true</h2>
<br>
<h2 class="aa d-inline" id="horarioDaAula1">true</h2>
<h2 class="aa d-inline" id="horarioDaAula2">true</h2>
<h2 class="aa d-inline" id="horarioDaAula3">true</h2>
<h2 class="aa d-inline" id="horarioDaAula4">true</h2>
<h2 class="aa d-inline" id="horarioDaAula5">true</h2>
<br>
That is, the more documents (mongodb) I have, the more H2 appears.
I tried to take the values of H2 by class (aa) but could not get these values
The way I tried to take the class:
for(i = 1; i<=5; i++){
var n = ".aa";
var myElement = $(n);
var myText = myElement.text();
console.log(myText)
if(myText == "true"){
myElement.html('<i class="fas fa-check-circle fa-xs" style="color: green"></i>');
}else{
myElement.html('<i class="fas fa-times-circle fa-xs" style="color: red"></i>');
}
}
I don’t understand, what’s the problem?
– JassRiver
The problem is that notice in the image, the values that were all true in the first line, were changed to icons, but in the second does not happen the same.
– André Cabral
The id is a unique identification. There can be no repetitions. If there are two elements with the same id the javascript can identify only 1. In your case you said you tried to get by the class aa. How did you do? the selector for classes is (.) instead of (#).
– Ryan Lemos
I’ll give you an example more or less of how it would look, and I’ll send it to you
– Ryan Lemos
The way I tried to get the contents by the class I edited the question.
– André Cabral
https://jsfiddle.net/dfsb387r/1/
– Ryan Lemos
Look there, what happens when you use the class selector, you select more than one element at a time. I used a function called
each
jquery going through all the elements that have been selected.– Ryan Lemos
https://api.jquery.com/each/
– Ryan Lemos
If I understand this question of jquery, the
$(this).text()
simply receives the content and may also be passing other content?– André Cabral
This refers to the selected html element. When you play it within the jquery($) function it is so that you can use the jquery functions with it. This text function is one of the jquery functions.
– Ryan Lemos
When you pass like this
$(this).text()
it returns the content of the element text. But if you pass some text in the parameter of this function it replaces the text of the html element$(this).text('modifica o texto do elemento')
.– Ryan Lemos