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

85
Vistas
Javascript / Jquery extract whole CLASS descriptor from a partial selection descriptor

I have a Javascript / Jquery function that controls groups of checkboxes.

The checkboxes are created on PHP form from a database call so I am iteratively going through a recordset and creating checkboxes in html.

For each checkbox I assign it a class of "checkboxgroup" + a numeric identifier to create a group of 'like' records. I end up with multiple checkboxes like this:

    <tr class="tablebody">
        <td><input name="contactresolveid2048" id="contactresolveid2048" type="checkbox" class="checkboxgroup0"/></td>
        <td>David&nbsp;Smith</td>
    </tr>
    <tr class="tablebody">
        <td><input name="contactresolveid19145" id="contactresolveid19145" type="checkbox" class="checkboxgroup0"/></td>
        <td>graham&nbsp;Foots</td>
    </tr>
    <tr class="tablebody">
        <td><input name="contactresolveid19146" id="contactresolveid19146" type="checkbox" class="checkboxgroup0"/></td>
        <td>Tom&nbsp;Silly</td>
    </tr>

As you can see, these 3 checkboxes have a class of 'checkboxgroup0'

The following function detects a click on ANY of the checkbox groups on a form (of which there may be many) and unchecks any checkboxes (belonging to the same group) that are not the clicked one.

    $('[class^="checkboxgroup"]').click(function() {
        var thisClass = $(this).attr('class');
        var $checkboxgroup = $('input.'+thisClass);
        $checkboxgroup.filter(':checked').not(this).prop('checked', false);
    });

Under most circumstances this works fine when the only class is 'checkboxgroup0'.

However when validation takes place JQuery validate appends a 'valid' or 'error' class to the class list of any fields that pass or fail validation, so I can endup having an .attr(class) of 'checkboxgroup0 valid'.

My question is this:

How do I return the whole class name of the partially selected class WITHOUT any extraneous classes?

By using the selector $('[class^="checkboxgroup"]') I need the whole part of that selector 'checkboxgroup0' and no other classes that may be assigned to it.

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

0

This issue you've encountered is one of the reasons why using incremental id/class attributes are not good practice.

To work around this issue with your JS you can instead use the same class on every checkbox. You can then group them by a data attribute instead. Using this method means that the number of classes on an element or their position within the class attribute string does not matter.

Try this example:

$('.checkboxgroup').click(function() {
  let $this = $(this);
  let $group = $(`.checkboxgroup[data-group="${$this.data('group')}"]`);
  $group.not(this).prop('checked', false);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr class="tablebody">
    <td><input name="contactresolveid2048" id="contactresolveid2048" type="checkbox" class="checkboxgroup" data-group="0" /></td>
    <td><label for="contactresolveid2048">David Smith</label></td>
  </tr>
  <tr class="tablebody">
    <td><input name="contactresolveid19145" id="contactresolveid19145" type="checkbox" class="checkboxgroup" data-group="0" /></td>
    <td><label for="contactresolveid19145">graham Foots</label></td>
  </tr>
  <tr class="tablebody">
    <td><input name="contactresolveid19146" id="contactresolveid19146" type="checkbox" class="checkboxgroup" data-group="0" /></td>
    <td><label for="contactresolveid19146">Tom Silly</label></td>
  </tr>
</table>

However, it's worth noting that what you're attempting to do can be far better achieved using HTML alone. Simply use a radio input and give them all the same name attribute, then you get the behaviour you're trying to create for free:

<table>
  <tr class="tablebody">
    <td><input name="contactresolve" id="contactresolveid2048" type="radio" /></td>
    <td><label for="contactresolveid2048">David Smith</label></td>
  </tr>
  <tr class="tablebody">
    <td><input name="contactresolve" id="contactresolveid19145" type="radio" /></td>
    <td><label for="contactresolveid19145">graham Foots</label></td>
  </tr>
  <tr class="tablebody">
    <td><input name="contactresolve" id="contactresolveid19146" type="radio" /></td>
    <td><label for="contactresolveid19146">Tom Silly</label></td>
  </tr>
</table>

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