How can I change values using JQuery?

1

I need to change some values in a pre-set options box using JQuery , an example of code :

<div id="div-archive-max-size">
Max size for archive :
<select name="archive-max-size">
<option value="10485760">10240 Mo</option>
<option value="5242880">5120 Mo</option>
<option value="4194304">4096 Mo</option>
<option value="3145728">3072 Mo</option>
<option value="2097152">2048 Mo</option>
<option value="1048576">1024 Mo</option>
<option value="524288">512 Mo</option>
<option value="262144">256 Mo</option>
<option value="204800">200 Mo</option>
<option value="102400">100 Mo</option>
</select>
</div>

The idea is to change the numerical value ( value="10485760" ) plus the description ( 10240 Mo ) to ( value="17485760" ) and ( 17 Gb ) respectively, and so on with the others ... with custom values and descriptions . It would be a script to use in'Tampermonkey' or similar .. Someone could give me an answer but honestly I do not know how I can apply the solution, the one they knew how to give me:

  

You only have to go through each option of select with the method .each() . To assign the value to option , use the function .val() and to assign the text .text() .

     

For example:

    function modificar()
{
  var data = [{val: "15485760", text: "15 GB"},{val: "otros valores", text: "otros valores"},{val: "otros valores", text: "otros valores 2"}];
  $("select[name='archive-max-size'] option").each(function(index, element){

    $(element).val(data[index].val)
    .text(data[index].text);

  });
}

Thank you very much already.

    
asked by Stack_qwerty 12.10.2017 в 20:41
source

1 answer

1

According to what you explain, the answer would be correct, although I would add a type check (in case the array is incomplete).

$(document).ready(function(){
  var valores = [[17485760,'17 Gb'],[223123,'223 Kb']];
  $('select[name=archive-max-size] option').each(function(i,item){
    if(typeof(valores[i]) != 'undefined'){
      $(item).val(valores[i][0]).text(valores[i][1]);
    }
  }
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div-archive-max-size">
Max size for archive :
<select name="archive-max-size">
<option value="10485760">10240 Mo</option>
<option value="5242880">5120 Mo</option>
<option value="4194304">4096 Mo</option>
<option value="3145728">3072 Mo</option>
<option value="2097152">2048 Mo</option>
<option value="1048576">1024 Mo</option>
<option value="524288">512 Mo</option>
<option value="262144">256 Mo</option>
<option value="204800">200 Mo</option>
<option value="102400">100 Mo</option>
</select>
</div>

Greetings.

    
answered by 12.10.2017 / 21:08
source