select picture by choosing date in datepicker


select picture by choosing date in datepicker



I would like to create a simple web page, where user can select a picture by choosing date in datepicker. The picture filenames consist date string. For example: img-2018-06-24.png. So far, I could get work this datepicker example:


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();

//selecting the button and adding a click event
$("#alert").click(function() {
//alerting the value inside the textbox
var date = $("#datepicker").datepicker("getDate");
alert($.datepicker.formatDate("dd-mm-yy", date));
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
<p>Alert the value: <button id="alert">Alert!</button>

</body>
</html>



which popups the selected date after clicking on the button. Instead, I would like to put a picture according to selected date. So, I need to somehow pass the date to filename string and use it in :



<img src= filename alt="myimage">


<img src= filename alt="myimage">



But, so far no luck...




3 Answers
3



Does following code work?


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();

//selecting the button and adding a click event
$("#alert").click(function() {
//alerting the value inside the textbox
var date = $("#datepicker").datepicker("getDate");
if(date) {
var imgSrc = 'img-' + $.datepicker.formatDate("dd-mm-yy", date) + '.png';
$('#show-image').show().attr('src', imgSrc);
}
});
});
</script>
</head>
<body>

<img src='' alt="myimage" id="show-image" style="display:none;">

<p>Date: <input type="text" id="datepicker"></p>
<p>Alert the value: <button id="alert">Alert!</button>

</body>
</html>





Yes, this worked for me. Thank you ~
– Milo Bio
Jul 1 at 10:25



You need to set src attribute of img inside the click event like this -


src


img


$("img").attr("src",$.datepicker.formatDate("dd-mm-yy", date));



If you want to change the alt attribute as well, you can do it the same way -


$("img").attr("alt",$.datepicker.formatDate("dd-mm-yy", date));



See working Fiddle



This how you can add src on image


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker(); //selecting the button and adding a click event
$("#alert").click(function() { //alerting the value inside the textbox
var date = $("#datepicker").datepicker("getDate");

alert($.datepicker.formatDate("dd-mm-yy", date));

var src= $.datepicker.formatDate("dd-mm-yy", date);
$("#image").attr("src",src+".jpg").show();

});
});
</script> </head> <body>
<img id="img" src="" style="display:none;">
<p>Date: <input type="text" id="datepicker"></p> <p>Alert the value: <button id="alert">Alert!</button> </body> </html>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API