Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

385
Visualizações
EJS : Adding styles to infinite to do items in a list by their id's

I am trying to add styles to To-do items on my web page using their id field.

<body>
<h1><%- dayType %></h1>
<ul class="tasks">
    <% for(i = 0 ; i < data.length ; i++){ %>
        <input id='label-<%-i+1%>' type='checkbox'/>
        <label for="label-<%-i+1%>"><li><%- data[i] %></li></label>
    <% } %>
    <li>
        <form action="/" method="post">
            <input type="text" placeholder="Enter To Do Item" name="items">
            <input type="submit" value="Submit">
        </form>
    </li>
</ul>
In the code where I am using for loops, there is a checkbox and label to which I am adding styles. My css looks like this:
#label-1:checked ~ label[for=label-1],
#label-2:checked ~ label[for=label-2],
#label-3:checked ~ label[for=label-3],
#label-4:checked ~ label[for=label-4],
#label-5:checked ~ label[for=label-5]{
  background: #6d335c;
  border-bottom: 1px solid #34495E;
  color: #d37b79;
}

When a checkbox of a to do item is checked, the corresponding to do item's style is changed. However, I am able to only change the style of a limited number of to do items because in css, we have to hard code the style for each To do item's id. I want the website to enter as many to do items as possible and their checkboxes to run perfectly. How to achieve this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you dont have to hard code CSS for every label. You can just add the properties to all children elements of your <ul class="tasks"> parent element.For e.g .tasks input {} will select all the input tags inside your tasks class.

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use a selector which will select all elements with an attribute starting with a specified string

[attr^=value]

see MDN

In your case:

*[id^="label-"]:checked ~ label[for^="label-"]{
  background: #6d335c;
  border-bottom: 1px solid #34495E;
  color: #d37b79;
}

However, unless there is a reason why you have to distinguish between those elements by id, I'd suggest you use a class instead. Then you can be absolutely sure you are getting the right set of elements.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda