I have this input with data-options attribute.
<input class="data" type="text" data-options="{maxDate:'0'}" />
I would like to load the datepicker using the data-options value as options. Now with the following code, doesn't work
$("input.data").each(function(){
var dateOptions=$(this).data('options');
$(this).datepicker(dateOptions)
});
but if I put the option on the js like in the following code, it works:
$("input.data").each(function(){
$(this).datepicker({maxDate:'0'})
});
https://jsfiddle.net/VixedS/exfLf6o9/
If is somebody can, I would prefer an answer without eval.
inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });
If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.
By default, for <input> elements, the datepicker calendar opens in a small overlay when the associated text field gains focus. For an inline calendar, simply attach the datepicker to a <div>, or <span> element.
When you call data
function, it returns string so you have to convert it to an object and then pass it to datepicker
and remove curly bracket from value of data-options
.
Solutions:
1- Use eval
Javascript
eval('({' + $(this).data('options') + '})')
HTML
data-options="maxDate:'0'"
2- Jquery .data
and surround your data attribute value by '
Javascript
$(this).data('options')
HTML
data-options='{"maxDate":0}'
3- use plugin or write custom function(the below code is written by @allenhwkim).
Javascript
function JSONize(str) {
return str
// wrap keys without quote with valid double quote
.replace(/([\$\w]+)\s*:/g, function(_, $1){return '"'+$1+'":'})
// replacing single quote wrapped ones to double quote
.replace(/'([^']+)'/g, function(_, $1){return '"'+$1+'"'})
}
jQuery.parseJSON(JSONize($(this).data('options')));
HTML
data-options="{maxDate:'0'}"
Note: all above solutions are tested and they works.
$("input.dataWithoutOptions").each(function() {
$(this).datepicker({
maxDate: '0'
})
});
$("input.data").each(function() {
var dateOptions = eval('({' + $(this).data('options') + '})');
console.log(typeof($(this).data('options'))); //String
console.log(typeof(dateOptions)); //Object
$(this).datepicker(dateOptions)
});
input {
display: block;
margin: 10px 0 20px;
padding: 5px;
}
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
This has options attr:
<input class="data" type="text" data-options="maxDate:'0'" />This is just a date pick:
<input class="dataWithoutOptions" type="text" />
Jsfiddle
Jquery data automatically parses JSON strings into objects. You just have to follow the directions from jQuery.parseJson()
http://api.jquery.com/jquery.parsejson/
Changing your Options From data-options="{maxDate:'0'}"
to data-options='{ "maxDate": 0 }'
Works wonders
Since in XHML you don't want to use single '
for properties, you can do the oposite and then replace the single quotes with double then parse the json response. { 'maxDate': 0 }
then .replace(/'/g, '"')
and use $.parseJSON()
$("input.dataWithoutOptions").each(function() {
$(this).datepicker({
maxDate: '0'
})
});
$("input.data").each(function() {
var dateOptions = $.parseJSON($(this).data('options').replace(/'/g, '"'));
$(this).datepicker(dateOptions);
});
input {
display: block;
margin: 10px 0 20px;
padding: 5px;
}
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
This has options attr:
<input class="data" type="text" data-options="{ 'maxDate': 0 }" />This is just a date pick:
<input class="dataWithoutOptions" type="text" />
@Lidaranis : Brings up a good point.
You can use escaped characters to avoid regex and parsing json. {"maxDate":0}
$("input.dataWithoutOptions").each(function() {
$(this).datepicker({
maxDate: '0'
})
});
$("input.data").each(function() {
var dateOptions = $(this).data('options');
$(this).datepicker(dateOptions);
});
input {
display: block;
margin: 10px 0 20px;
padding: 5px;
}
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
This has options attr:
<input class="data" type="text" data-options="{"maxDate":0}" />This is just a date pick:
<input class="dataWithoutOptions" type="text" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With