Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot call methods on slider prior to initialization attempt to call method 'value'

I am using the following code for slider

Css and Jquery:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

ASPX:

<p>
   <label for="amount">Value:</label> 
   <input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" />%
</p> 
<div id="slider-vertical" style="height: 15px; width:800px"></div>
<script type="text/javascript">
    $(function () {
        $("#slider-vertical").slider({
            orientation: "horizantal", range: "min", min: 0, max: 100, value: 60,
            slide: function (event, ui)
            { $("#amount").val(ui.value); }
        });
        $("#amount").val($("#slider-vertical").slider("value"));
    });
</script>

I am trying to set the value for slider:

function UpdateSlider(PhaseInStatusReportVal) {
    $("#slider-vertical").slider("value").val(PhaseInStatusReportVal);
}

I am calling above code from something like this :

ClientScript.RegisterStartupScript(Me.GetType(), "updatetheslider", "UpdateSlider('" + BCOMSPackageValidationList(0).PhaseInStatusReport.ToString() + "')", True)

but it's throwing the above error.

Can anyone please help me?

like image 940
Kapil Avatar asked Jun 25 '13 00:06

Kapil


3 Answers

No answers explain the issue. The problem is that you're trying to create an event listener on a DOM element before the document is loaded:

If your chunk of code is supposed to run as soon as the page is loaded you can wrap it in a function to run as soon as the document loads:

document.onload=function(){
// Your Code Here
}

If the chunk of code runs dynamically inside a function you can simply wrap your code inside a check to make sure the document is loaded:

if (document. readyState === 'complete') {
//Your Code Here
}
like image 84
Alexander D'Attore Avatar answered Oct 20 '22 08:10

Alexander D'Attore


Have you considered a different approach. Setup a hidden field with the initial value -> populate the value from the code-behind, then do something like:

$(function () {
    $("#slider-vertical").slider({
        value: $("#hiddenFieldWhatever").val(),
        // setup the rest ...
    });  
});

I hope I understood your requirements correct.

like image 8
Dimitar Dimitrov Avatar answered Oct 20 '22 06:10

Dimitar Dimitrov


var slider = $("#slider-vertical");

var a = function() {
    if(slider.slider("instance")) {
        slider.slider('value', 1);
        console.log('set value');
    } else {
        console.log('ERROR: cannot set value prior to initialization');
    }
}

var b = function() {
    slider.slider();
    console.log('initialized');
}

a();
b();
a();
like image 6
Abdul Ahad Avatar answered Oct 20 '22 08:10

Abdul Ahad