Question

I have a checkbox and 2 number input fields:

<input name="exercise[hold]" type="hidden" value="0"><input class="te-cb" id="exercise_hold" name="exercise[hold]" type="checkbox" value="1">
<input class="exerciseReps input-mini" id="exercise_number_of_reps_in_set" min="0" name="exercise[number_of_reps_in_set]" placeholder="Reps" type="number" style="display: none; ">
<div class="input-append te-len" style="display: inline-block; ">
    <input class="exerciseLength input-mini" id="exercise_length" min="0" name="exercise[length]" placeholder="Length" type="number">
    <span class="add-on">sec</span>
</div>

One input field is hidden with js:

$('.te-len').hide()

In form above there is also text input field with auto competition which gets the data with ajax (that is not relevant here). With jquery I am setting the values of input fields of form with data I got from server.

I also show hidden field if value for checkbox is true:

if data[selected].hold
    $('.exerciseReps').hide()
    $('.te-len').show()

and that works

Every field is set correctly accept the hidden field, that field is empty.

The weird thing is that I placed alert of that hidden field's value after it has been set and alert shows the value that should have been set but the value is not visible

$('.te-len').val( data[selected].length )
alert $('.te-len').val()

Note: the js code is actually coffescript.

Was it helpful?

Solution

I think the problem is because you are setting value to a div. Although use of val works, this function is primarily used on form elements such as input, select and textarea.

If you mean to replace the content of the div.te-len then you could use either html or text:

$('.te-len').html( data[selected].length )

But it sounds like you are trying to show data[selected].length value in the input.exerciseLength numeric field. For that you should be using:

$('#exercise_length').val( data[selected].length )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top