Bacter Bacter - 5 months ago 17
jQuery Question

Javascript - change html element in each loop

Dumb question, I'm sure, but I don't know the answer.

I've got a field that I populated this way

<div class="field">
<%f.label :client_id %>
<%= f.select :client_id,
options_for_select(Client.all.map{ |p| [p.appointment_record, p.id]}), # your options for this select box
include_blank: 'Select Client'
%>


So when I get

clients = $('#appointment_client_id').html()


it's

<option value="">Select Patient First, Please</option>
<option value="1">Guy, First - Programmer</option>
<option value="2">Guy, Second - Code Monkey</option>


What I want is to change those value tags. This list is generated dynamically.

How could I change the value tags in an .each loop so that it sticks?

Answer

It is not clear at all what you want to change or from what source.

Assuming you want to change <option> values you can use val(function) to loop through collection. The arguments of the function are index and current value

Example:

$('#appointment_client_id').children(':gt(0)').val(function(i, currVal){
    return currVal ? currVal * 10 :'';
});

DEMO