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

232
Vistas
Show check box value in a data-files attribute

I'm doing the download select multi files function. I have a question about how the values in the checkbox I selected to put into the data-files attribute. At present, what I can only do is to put the values in the checkbox in the input.

Below is my coding:

$('#multiselect-drop input').change(function() {
  var s = $('#multiselect-drop input:checked').map(function() {
    return this.value;
  }).get().join(' ');
  $('#results').val((s.length > 0 ? s : ""));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" id="results" style="width:60%;">

<div id="multiselect-drop">
  <input type="checkbox" value="file/john.jpeg">
  <input type="checkbox" value="file/important.pdf">
  <input type="checkbox" value="file/company.pdf">
</div>

<button id="download-button" class="btn btn-primary btn-xs" data-files="">Download</button>

The output result show in below, it will follow my tick to select the values in the input.

output1

Actually, I want the result is the values in the checkbox I selected put into the data-files attribute. Like below the sample result:

<button id="download-button" class="btn btn-primary btn-xs" data-files="file/john.jpeg file/important.pdf file/company.pdf">Download</button>

Hope someone can guide me on how to solve this problem. Thanks.

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

0

First you should using attr() jQuery method, try setting it as an attribute:

$('#download-button').attr('data-files', (s.length > 0 ? s : ""));

Then become:

$('#multiselect-drop input').change(function() {
  var s = $('#multiselect-drop input:checked').map(function() {
    return this.value;
  }).get().join(' ');
  $('#results').val((s.length > 0 ? s : ""));
  $('#download-button').attr('data-files', (s.length > 0 ? s : ""));                               
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Not everything has to be accessed, read or written in a jQuery way. Make use of an HTMLElement's dataset property ...

$('#multiselect-drop input').change(function() {

  let s = $('#multiselect-drop input:checked').map(function() {
    return this.value;
  }).get().join(' ');

  $('#results')[0].value = s;
  $('#download-button')[0].dataset.files = s;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" id="results" style="width:60%;">

<div id="multiselect-drop">
  <input type="checkbox" value="file/john.jpeg">
  <input type="checkbox" value="file/important.pdf">
  <input type="checkbox" value="file/company.pdf">
</div>

<button id="download-button" class="btn btn-primary btn-xs" data-files="">Download</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using this way you can set the value to data-files and get the value from it.

 $('#download-button').attr('data-files', (s.length > 0 ? s : ""));
    var a = $('#download-button').data('files'); 

Here is the working example hope it will helpful

         <!DOCTYPE html>
            <html>
            <head>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            </head>
            <body>


            <input type="text" id="results" style="width:60%;">

            <div id="multiselect-drop">
              <input type="checkbox" value="file/john.jpeg">
              <input type="checkbox" value="file/important.pdf">
              <input type="checkbox" value="file/company.pdf">
            </div>

            <button id="download-button" class="btn btn-primary btn-xs" data-files="">Download</button>
            <script>
            $('#multiselect-drop input').change(function() {
              var s = $('#multiselect-drop input:checked').map(function() {
                return this.value;
              }).get().join(' ');
              $('#results').val((s.length > 0 ? s : ""));

              $('#download-button').attr('data-files', (s.length > 0 ? s : ""));
              
            });
            $('#download-button').on('click',function(){
             var a = $('#download-button').data('files'); 
               alert(a)
            });
            </script>
            </body>
            </html>

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