Javascript get Date And Selected Option Text
In this tutorial we will learn about how to get Date from the textbox and displayed as Date as well as how to get the selected option text and displayed in Javascript.
This tutorial explains you that how to display the date and the selected option text in Javascript. This example explains how to write the Javascript code for displaying the date and the selected option text into HTML.
Example
Here I am giving a simple example which will demonstrate you how to get the date and selected option value using id in html form and display these value in Javscript. To accomplish this task I will create a Html page where I will write the script for taking the value from html form using id. And at html page I will use the html tags for taking date and select and option tags for providing the dropdown list of options.
Source Code
<html> <head> <script> function getData(){ var d = document.getElementById("dt").value; var dob = new Date(); Date.parse(d); alert(dob); var g=document.getElementById("gd"); var txt=g.options[g.selectedIndex].text; alert(txt); } </script> </head> <body> <form action="#" onsubmit="getData();"> <table> <tr> <td>Enter DOB </td> <td><input type="text" id="dt"/></td> </tr> <tr> <td>Select Gender </td> <td> <select id="gd"> <option>Male</option> <option>Female</option> </select> </td> </tr> <tr><td></td> <td><input type="submit" value="submit"/></td> </tr> </form> </body> </html>
Output
When you will execute the above code you will get the output as follows :
When you will click on submit button then the output will be as follows :
When you will click on ok button then the output will be as follows :