How to copy a value within a span

Asked

Viewed 652 times

2

I need to copy a value from inside a span that is generated by a slider, it whenever it moves the slider it is changed. It has how I put it within a value of an input?

I tried that and it didn’t work:

<h4>Valor do consórcio: <span class="slider-value quote-form-element valor-carro1" data-name="Valor | Automóvel" name="Valor" data-slider-id="consorcio-auto">R$ <span id="THAT_VALUE"></span></span>
                            </h4>
<div class="slider" data-slider-min="20000" data-slider-max="100000" data-slider-start="23192" data-slider-step="1000" data-slider-id="consorcio-auto"></div>

<h4>Seus dados:</h4>
<input type="hidden" id="THAT_FIELD" name="THAT_FIELD" value="" />
<h4>Seus dados:</h4>
<input type="hidden" id="valorcarro" name="valorcarro" value=""  />

Script:

$(function(){
var valorcarro = $('#THAT_VALUE').html();
$('#THAT_FIELD').val(valorcarro);
});

The problem is that with this script the value in the input is empty, because the text of the span is only generated later, and the user can still change it.

Example in this page on the "Simulation" button in the menu.

  • You have to do this every time the user changes the slider, take a look at the change event of this plugin you are using. What can help you, but it is gambiarra is to place this function of yours within a $(Document). change(Function() { //Function here });

1 answer

1

You can use the following to get the value of the slider:

<!-- adicionei um id para simplificar o exemplo -->
<div class="slider" id="slider" ...... >


$('#THAT_FIELD').val($('#slider').slider('option', 'value'));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.