Html input field not registring on first click

Viewed 153

I am having issues with begin date field. the first time that the page loads, if you click on the begin date field the calender is not poping up, however if I click on any other field/label and than click back on begin date field, it works just fine. I have tried the following with no success:

1- I have tried using onclick and onchange to invoke the js function 2- I have tird adding a listener on page load to the beginDate element. 3- Tried moving focuse to another element on page load.

var tripsByCategory = {
    1: ["3", "5"],
    2: ["2", "3", "4"],
    3: ["5", "7"]
}

function changecat(value) {
    if (value.length == 0) document.getElementById("duration").innerHTML = "<option></option>";
    else {
        var catOptions = "";
        for (categoryId in tripsByCategory[value]) {
            catOptions += "<option>" + tripsByCategory[value][categoryId] + "</option>";
        }
        document.getElementById("duration").innerHTML = catOptions;
    }
}

function datePicker() {

    $('#beginDate').datepicker({dateFormat: "yy-mm-dd"}); 

}


function clearPage() {
     var form = document.getElementById("costForm");
     form.onsubmit = form.reset(); 
     document.addEventListener("click", function() {
         datePicker();
        //alert("yay");
        }, true);

        document.getElementById("beginDate").addEventListener("click", function() {
            datePicker();
        }, false);
}
body{
background-color: #7fc7d6;
}

.sectionTitle{
    text-align: center;
}
.column3 {
  float: left;
  width: 100%;
  padding: 2px;
}
    
.row::after {
  display: table;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>test</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="index.js"></script>
<link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css'  rel='stylesheet'> 
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">  </script> 
<script src=  "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js">  </script> 
</head>
<body onLoad="clearPage()">

    <div class="row">
        <h2 class="column3">!Testing</h2>
        <form action="http://localhost:8080//places" method=GET id="costForm">
            <div class="column3">
                <label for="numberOfPeople">Number of Travelers:</label>
                <input type="number" id="numberOfPeople" name="numberOfPeople" min="1" max="10" required />
            </div>
            
            <div class="column3">
                <label for="beginDate">Begin Date:</label>
                <input type="text" name="beginDate" id="beginDate" value="YYYY-MM-DD" required />
            </div>      
            <div class="column3">
                <label for="hikeOption">Choose a Destenation:</label>
                <select name="hikeOption" id="hikeOption" onclick="changecat(this.value);" required >
                    <option value="" disabled selected>Select</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
            </div>
                <div class="column3">
                <label for="duration">Choose a Duration:</label>
                <select name="duration" id="duration" required>
                    <option value="" disabled selected>Select</option>
                </select>
            </div>
            <div class="column3">
                <input type="SUBMIT" value="Get Total Cost">
            </div>
        </form>
    </div>

</body>
</html>

2 Answers

Since I see that you're using jQuery my suggestion would be to use an event listener.

Create a document ready function:

$(document).ready(function(){

});

And inside of that function, add the listener function

$(document).ready(function(){
    $('#beginDate').click("off").click(function(){
        //Process data
    });
});

EDIT: In addition, you may want to run your code through a syntax checker such as: https://validator.w3.org/

I bring this one up because your syntax for html select is incorrect, it should be

<select>
    <option value="val1">Option Label</option>
</select>

It seems you are self-closing the tag, which can cause issues.

Use the Event mousedown instead, that solve the issue. This is a common issue not related to jquery. But it might not work flawlessly on mobiles, you will have to add touch events as well, see https://developer.mozilla.org/en-US/docs/Web/API/Touch_events.

var tripsByCategory = {
    1: ["3", "5"],
    2: ["2", "3", "4"],
    3: ["5", "7"]
}

function changecat(value) {
    if (value.length == 0) document.getElementById("duration").innerHTML = "<option></option>";
    else {
        var catOptions = "";
        for (categoryId in tripsByCategory[value]) {
            catOptions += "<option>" + tripsByCategory[value][categoryId] + "</option>";
        }
        document.getElementById("duration").innerHTML = catOptions;
    }
}

function datePicker() {

    $('#beginDate').datepicker({dateFormat: "yy-mm-dd"}); 

}


function clearPage() {
     var form = document.getElementById("costForm");
     form.onsubmit = form.reset();
       document.getElementById("beginDate").addEventListener("mousedown", function() {
            datePicker();
        }, false);
}
body{
background-color: #7fc7d6;
}

.sectionTitle{
    text-align: center;
}
.column3 {
  float: left;
  width: 100%;
  padding: 2px;
}
    
.row::after {
  display: table;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>test</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="index.js"></script>
<link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css'  rel='stylesheet'> 
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">  </script> 
<script src=  "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js">  </script> 
</head>
<body onLoad="clearPage()">

    <div class="row">
        <h2 class="column3">!Testing</h2>
        <form action="http://localhost:8080//places" method=GET id="costForm">
            <div class="column3">
                <label for="numberOfPeople">Number of Travelers:</label>
                <input type="number" id="numberOfPeople" name="numberOfPeople" min="1" max="10" required />
            </div>
            
            <div class="column3">
                <label for="beginDate">Begin Date:</label>
                <input type="text" name="beginDate" id="beginDate" value="YYYY-MM-DD" required />
            </div>      
            <div class="column3">
                <label for="hikeOption">Choose a Destenation:</label>
                <select name="hikeOption" id="hikeOption" onclick="changecat(this.value);" required >
                    <option value="" disabled selected>Select</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
            </div>
                <div class="column3">
                <label for="duration">Choose a Duration:</label>
                <select name="duration" id="duration" required>
                    <option value="" disabled selected>Select</option>
                </select>
            </div>
            <div class="column3">
                <input type="SUBMIT" value="Get Total Cost">
            </div>
        </form>
    </div>

</body>
</html>

Related