Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
What's the most elegant way to perform the same actions on two different elements in two overlapping scenarios?

I have two <p> elements, $elem1 and $elem2. Collectively, we can call them $elems. I want to make each of their text red in color under the following scenarios:

$elem1 when the screen size is under 600 pixels wide $elem2 as soon as the page loads (aka, it should turn red instantly and stay red)

Now, I could do something like:

$elem2.css('color', 'red');

if (screenSize < 600) {
  $elem1.css('color', 'red');
}

Which is manageable, but what if instead of making each element's text red, I wanted to add 20 attributes, plus some on click events and a CSS class. Now the above code has ballooned up to a whole lot of duplicate code.

My question is, how can I elegantly write jQuery/Javascript that encompasses both $elem1 and $elem2's scenarios so that the "add attribute/event/addClass" code is only spelled out once?

I've tried spelling out all the added attributes/events/addClass for each scenario separately, and that does work, but I know it's going to cause maintenance issues in the future when I forget to replace something in one and not the other.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can pass an object to .attr() to pass multiple attributes and values

https://api.jquery.com/attr/#attr-attributes

Using this, you could have shared keys

const shared = { checked: "checked", "data-id": id }

$elem2.attr(shared);
$elem2.attr({
  // elem2 specific
});

if (screenSize < 600) {
  $elem1.attr(shared);
  $elem1.attr({
    // elem1 specific
  });
}


For what it's worth, given that you are concerned about code duplication, maintenance and managing presentation change, a declarative/component-based alternative library like React might solve your problems

about 4 years ago · Juan Pablo Isaza Denunciar

0

Couldn't you give each element a class name and update css that way? For example say we assign elem1 and elem2 a css class called aCssClassToUpdate. Then do following:

const classesToUpdate = document.querySelectorAll('.aCssClassToUpdate');
classesToUpdate.forEach(classToUpdate => {
  classToUpdate.style.color = 'red';
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda