Pass javascript json data with jquery
I have build this function:
function currentDate() {
var date;
var currentDate = new Date();
var day = curre开发者_Python百科ntDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
// if the month length = 1 add "0" to ir
if (currentDate.getMonth().toString().length == 1) {
month.toString();
month = "0" + month;
}
// if the day length = 1 add "0" to ir
if (currentDate.getDate().toString().length == 1) {
day.toString();
day = "0" + day;
}
var ISO = year + "-" + month + "-" + day;
var EUR = day + "/" + month + "/" + year;
return date = {
"ISO": ISO,
"EUR": EUR
};
}
When I click on a button I want the ISO date or the EUR date to be passed into a text input. How can this be done???
Thank you..
<html>
<head>
<Script>
// your sript goes here
</script>
</head>
<body>
...
<input type="text" id="date" size="15" /><br />
<input type="button" onclick="document.getElementById('date').value=currentDate().ISO;" value="ISO" />
<input type="button" onclick="document.getElementById('date').value=currentDate().EUR;" value="EUR" />
,...
</body>
</html>
DEMO
Or jQuery: DEMO
Try out this fiddle example
<button data-type="EUR">set date EUR</button>
<button data-type="ISO">set date ISO</button>
<input id="date" type="text" />
JS:
$('button').click(function(e){
var d = currentDate(),
type = $(this).data().type;
$('#date').val(d[type]);
});
When defining an object the key does not need to be in quotes.
<input id="cdate" />
<input type="button" onclick="$('#cdate').val(currentDate().ISO)" />
精彩评论