1

I have a javascript that returns a value whenever I choose an option from a dropdown menu.

   $(document).ready(function() {
        function calculateVals() { 
            //dropdown menus      
             var valuestart = $("select[name='timestart']").val();
             var valuestop = $("select[name='timestop']").val();

             //create date format          
             var timeStart = new Date("01/01/2007 " + valuestart).getHours();
             var timeEnd = new Date("01/01/2007 " + valuestop).getHours();

             var hourDiff = timeEnd - timeStart;                         

             return hourDiff;  

        }
        $("select").change(calculateVals);
        calculateVals();
    });

The script above returns a number which I need to add to a jquery plugin below. I need to add it to the max option but I get the error calculateVal is not defined.

$(function(){
    $("#slider").slider({
        range: "min",
        value:  8,
        min: 1,
        max: calculateVals(), //my attempt
        step: 1,
        slide: function(event, ui) {
            $("#amount").text(ui.value);
        },
         stop: function(event, ui) {
            $("#notifications").val(ui.value);
        }
    });
    $("#notifications").val( $("#slider").slider("value") );
});
1
  • @gdoron thanks i dont see it tho haha Commented Jun 14, 2012 at 22:35

2 Answers 2

2

Move the function to the global scope:

function calculateVals() {       
    //code here...
    return someResult;                                           
}

$(document).ready(function() {
        $("select").change(calculateVals);
    calculateVals();
});

And even better, learn what does DOM ready means, as you misuse it.

  • You should have only one ready handler.
  • Only code relevant to DOM elements should be there.

Update:

You can later on change the slider max with:

$( "#slider").slider("option", "max", calculateVals());
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you! Would some extra code make this dynamic? When i click my dropdown menu and choose an option nothing happens.
@CyberJunkie. I got the feeling your problem lies here //code here... :) and anyway I don't know what you call dynamic, nor what exactly is the current problem.
you are right! I added the missing code to the answer. It calculates an hour difference between 2 times i select from the 2 dropdown menus and returns the result. The result I'm trying to add to the jquery function in max option which alters a slider. The function executes only when i load the page
@CyberJunkie. You can later on change the slider max with $( "#slider").slider("option", "max", calculateVals());
do you mean like this? $("select").change(function() { $( "#slider" ).slider("option", "max", calculateTime()); }); I tried with no success..
|
2

Try defining the function out side the ready block:

function calculateVals() { 

             //code here...

             return someResult;                                          
}



$(document).ready(function() {
    $("select").change(calculateVals);
    calculateVals();
});

1 Comment

Thanks I tried. I don't get an error but nothing happens when I click the select menu. someResult should return a number which should make something happen on change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.