I have written something like below. onclick of div with id "PLUS" I am getting the following error:
cannot call methods on slider prior to initialization attempted to call method 'value'
<div id="PLUS" class="PLUS"></div>
<script>
$(function() {
$(".slider").slider({
animate: true,
range: "min",
value: 18,
min: 18,
max: 70,
step: 1,
slide: function(event, ui) {
$("#slider-result").html(ui.value);
document.getElementById(findElement('ageId')).value = ui.value;
},
//this updates the hidden form field so we can submit the data using a form
change: function(event, ui) {
$('#hidden').attr('value', ui.value);
}
});
$(".PLUS").click(function() {
var value = $("#slider-result").slider("value"),
step = $("#slider-result").slider("option", "step");
$("#slider-result").slider("value", value + step);
});
});
</script>
Any help is appreciated.
If we check error in detail you will notice that it says you are trying to call the
value
method before the initialization of slider plugin.Reason:
Actually JavaScript is an interpreted language, and it doesn't wait for first command to execute and finish. That's why your
$(".slider").slider({
and$(".PLUS").click(function() {
lines run at same time and the error occurs.Solution:
You can put your code in
setTimeout
function here is an example given below.I hope this will help you/someone.
Regards,
The error is caused because
$("#slider-result")
is not the element initialized as slider and you're trying to execute slider widget methods on it instead of$(".slider")
which is the actual slider.Your code should be
i had a similar problem. your block here
just keep it under
ie.
hope this works.
The best way I found to achieve this is to use the event from the ON function from the slider library to get the value. Ex:
Regards
You have used
$(".slider").slider()
at the time of initializing and$("#slider-result").slider()
at the time of getting the value some plugins work on selector you have used at the time of init, so try that.