Rafał Kokosiński Rafał Kokosiński - 3 months ago 10
PHP Question

Exchange text label to select value

I have a box entitled by label "Data sprzedaży":

<div class="formField">
<label for="invoice_invoice_sale_date">Data sprzedaży</label>
<?php draw_date_selects('invoice_sale_date', date('d'), date('m'), date('Y'))?>

<div class="clearer"></div>
</div>



I would like to put there dropdown with 3 options.

If I choose one of 3 options, it will replace the default label.

Answer

So the first thing you need to do is pass the option value to a variable, from there you can replace the text of the label with the option value:

$(function() {
    $('select').on('change', function() {
	labelText = $(this).val(); // Get value of selected option
        $('label').text(labelText); // Pass selection option value into label
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="formField">
    <label for="invoice_invoice_sale_date">Data sprzedaży</label>
    <?php draw_date_selects('invoice_sale_date', date('d'), date('m'), date('Y'))?>

    <div class="clearer"></div>
</div>

<select>
  <option value="" disabled selected>Select an option</option>
  <option>Option 1</option>
  <option>Option 2</option>
  <option>Option 3</option>
</select>

Comments