Q:

Javascript runs in browser, not in phonegap app with cordova

This code below is for a form that needs to save some values into local storage, I have got it to work in a browser, but when I load this thing up in xcode/cordova, it wont fire the function. I tried debugging, hence the many, many alerts, I tried in DWCS6 with live view, but no avail, I can't seem to get it to run. Can you please find the error?
The function saveSpanning() has an if loop and when it goes out of the if-then-else loop it doesn't continue in xcode simulator. In the browser it does continue.
update: what is does: there's a slider, that produces a value, this value needs to be saved in local storage along with two other values (generated by JavaScript), namely: the date and the record count. So three values in all.
The script runs, using jquery mobile, the button to start this function works, I have use document.ready instead onBodyLoad, it basically works, but the function saveSpanning just does not go further in phonegap/ios/xcode simulator or device.
function saveSpanning() {
    alert("saveSpanning gestart!");
    var inputSpanning = document.getElementById("valSliderSpanning").value;
    alert("input spanning = " + inputSpanning);

    //For Time
    var mes_time = document.getElementById("tijdSpanning").value;
    var mestimearr = mes_time.split(":");
    //For Date
    var mes_date = document.getElementById("datumSpanning").value;
    var mesdatearr = mes_date.split("-");
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    var curr_hours = d.getHours();
    var curr_min = d.getMinutes();
    var curr_sec = d.getSeconds();
    //newDate = curr_year + "/" + curr_month + "/" + curr_date + " " + curr_hours + ":" + curr_min + ":" + curr_sec
    // origienel opmaak datum newDate = mesdatearr[0] + "/" + mesdatearr[1] + "/" + mesdatearr[2] + " " + mestimearr[0] + ":" + mestimearr[1] + ":00";
    newDate = mesdatearr[0] + "/" + mesdatearr[1] + "/" + mesdatearr[2];

    alert("deze datum wordt opgelsage: " + newDate);

    //var itemId = newDate.getTime(); //creates a unique id with the milliseconds since January 1, 1970
    var itemId = "spanningKey";
    var values = new Array();
    values.push(newDate); //push each value into our values array
    values.push(inputSpanning); //push each value into our values array
    //alert(inputSpanning);

    var spanningCountVal = localStorage.getItem('spanning_count');

    //alert(spanningCountVal);
    if (spanningCountVal == null) {
        spanningCountVal = 1;

        alert("spanningCountVal was null, en wordt dus nu 1: " + spanningCountVal);

    }
    else {
        spanningCount = parseInt(spanningCountVal) + 1;
        alert("zit nu in de else loop: " + spanningCount);

    }

    alert("uit de ifthenelseloop, spanningCount = " + spanningCount);

    itemId = itemId + '-rec-' + spanningCount;
    alert("itemid: " + itemId);
    alert("spanningCountVal: " + spanningCount);

    localStorage.setItem("spanning_count", spanningCount); //store the item in the database
    localStorage.setItem(itemId, values.join("|")); //store the item in the database
    alert("Successfully Saved.");
}

$(document).ready(function() {

    $("#button").click(function() {
        alert("hallo functie");
    });

    $("p").text("The DOM is now loaded and can be manipulated.");

    $('#button2').click(function() {
        alert('Button has been clicked');
    });
    $('#knopje').click(function() {
        saveSpanning();
    });
});​

A:

document.ready is called when all the DOM Elements are loaded, and accessible. In a browser this typically is a good time to start executing code that requires the DOM to be ready.
However in Phonegap/Cordova, there are a number of steps that run after the document.ready event is fired, including connecting to the debugging console.
Instead, you'll need to wait for the deviceready event to know when phonegap/cordova is fully loaded and ready to be executed. Inside $(document).on('ready') you'll need to add an event listener for deviceready which should fire your methods. 

