Yang
Yang

Reputation: 6892

jquery (JQM) refresh a form element

I'm trying to set the default value of a form element by calling some js code, but the input did not seem to get refreshed.

<li data-role="fieldcontain">
<label for="defcal">Purchase Date</label>
<input name="defcal" type="date" data-role="datebox" id="defcal"/>
</li>

function onDeviceReady() {
var today = new Date();
var dayofmonth = today.getDate();
var dayofweek = today.getDay();
var year = today.getFullYear();
var month = today.getMonth();
var monthstring = month;
if(month < 10){
    monthstring = "0"+monthstring;
}

$('#defcal').val(year+'-'+month+'-'+dayofmonth);

}

Someone suggested using $('#defcal').textinput(); to refresh the input but it didn't work either.

Upvotes: 1

Views: 402

Answers (2)

shanabus
shanabus

Reputation: 13115

You may want to check that the onDocumentReady function is being called by adding an alert() to it or something because this code works in jsfiddle when called as the document ready function or $(function() { }); block.

Example: http://jsfiddle.net/p3GbM/

In this example I also set the #devcal value using the monthstring as it looks like you intended it.

Hope this helps!

Upvotes: 1

Jerome Ansia
Jerome Ansia

Reputation: 6884

Depends when do you want to call this function is it's on some elts change do something like this :

    $(function() {
    $('#ID_ELTS').change(function() {

  var today = new Date();
    var dayofmonth = today.getDate();
    var dayofweek = today.getDay();
    var year = today.getFullYear();
    var month = today.getMonth();
    var monthstring = month;
    if(month < 10){
        monthstring = "0"+monthstring;
    }

    $('#defcal').val(year+'-'+month+'-'+dayofmonth);
    });

});

Upvotes: 1

Related Questions