Help with on click on mobile <select> tag
https://youtu.be/0eTAQTmgTC0
This is my problem with the website I am working on for inventory of Magic cards.
I don't want mobile users to have to click on the option they want twice to get the data to load. If you watch the video you can see what I mean. Here is my code below.
- <!doctype html>
- <html lang="en">
- <head>
- <title>
- Magic Inventory
- </title>
- <meta charset="utf-8">
- <link rel="stylesheet" type="text/css" href="css/table.css">
- <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
- <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
- <link rel="stylesheet" type="text/css" href="css/Style.css">
- <link rel="stylesheet" type="text/css" href="css/table.css">
- <script type='application/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
- <style>
- tr.foilInStock{
- background-color: #9AD8ED;
- }
- tr.outOfStock{
- background-color: #C4C4C4;
- }
- .table_margin {
- margin-top: 3em
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="leftNavDiv col-md-3">
-
- <h1>Search</h1>
-
- <input type="text" id="inputText-cardNameSearchTextBox-leftNavDiv" placeholder="Spore Frog" style="width:260px;" class="form-control"/><br />
- <select id="select-cardListSelector-leftNavDiv" size="10" style="height:200px; width:260px;" class="form-control"></select><br />
-
- </div>
- <div class="bodyDiv col-md-9">
- <table id="div-cardsDisplayTable-bodyDiv" data--tbody-cardlist-bodydiv="" class="table table-bordered table_margin">
- <thead>
- <tr>
- <th>Name</th>
- <th>Set</th>
- <th>Quantity</th>
- </tr>
- </thead>
-
- <tbody data--tbody-cardlisttablebody-bodydiv="" id="div-cardsDisplayTableBody-bodyDiv">
-
- </tbody>
- </table>
- </div>
- </div>
- </div>
- <script>
- // ToC: search
- /* ---------------------------
-
- A) UI Functionality:
- 1. cardNameSearchTextbox_keyup
- 2. cardNameUL_Populate
- 3. cardNameSelected
-
- B) Event Listeners:
- 1. card name selected
- C) Global:
- 1. This function is for the delay of when a user is typing in the card name box before it ajax searches
- D) Document Ready:
- 1. card search text box, seeking keystrokes
- 2. prevent "Enter" keystroke from submitting in text input
- */
- /////////////////////////////////////////////////////////////////////// A
- // [A] UI Functionality /////////////////////////////////////////////// A
- ////////////////////////////////////////////////////////////////////// A
- //
- /*--------------------------------------
- Updates:
- 1/24/17:
- -Added á/Æ/â to Unescape
- */
- //
- function htmlUnescape(value) {
- return String(value)
- .replace(/quot;/g, '"')
- .replace(/'/g, "'")
- .replace(/u0026#39;/g, "'")
- .replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/&/g, '&')
- .replace(/u0026#225;/g, 'á')
- .replace(/u0026#198;/g, 'Æ')
- .replace(/u0026#226;/g, 'â')
- .replace(/u0026amp;/g, '&');
- }
- // [A.1] cardNameSearchTextbox_keyup
- /*
- This calls the delay function and will result in the cardNameUL_Populate function being called after the delay has passed.
- */
- function cardNameSearchTextbox_keyup () {
- delay(function () {
- if ($('#inputText-cardNameSearchTextBox-leftNavDiv').val().length > 2) {
- cardNameUL_Populate();
- } else {
- $('#select-cardListSelector-leftNavDiv').empty();
- };
- }, 500);
- };
- // [A.2] cardNameUL_Populate
- /*
- This function gets called to populate the list of cards for search
- */
- function cardNameUL_Populate () {
- // ajax
- $.ajax({
- type: "POST",
- url: "127.0.0.1/WebService.asmx/loadCardsBySearch",
- data: '{search: "' + $('#inputText-cardNameSearchTextBox-leftNavDiv').val() + '"}',
- contentType: "application/json; charset=utf-8",
- dataType: "json",
-
- success: function (response) {
-
- var JSONresponse = JSON.stringify(response).substring(6, JSON.stringify(response).length - 2);
- // error check
- if (JSONresponse.substring(0, 5) == "Error") {
- } else if (JSONresponse == "") {
- // if no errors
- } else {
-
- JSONresponse = JSONresponse.replace(/\\/g, '');
- var jsonObject = $.parseJSON(JSONresponse);
-
- $('#select-cardListSelector-leftNavDiv').empty()
- $.each(jsonObject, function (i, obj) {
- $('#select-cardListSelector-leftNavDiv')
- .append($('<option>', { value: htmlUnescape(obj.cardName), text: htmlUnescape(obj.cardName) }));
- }); // end $.each
- }; // end if
- }, // end success
- failure: function (msg) {
- alert(msg);
- }, // end failure
- complete: function (jqXHR, textStatus) {
- // alert('complete')
- }
- }); // end $.ajax
- };
- // [A.3] cardNameSelected
- /*
- */
- function cardNameSelected (cardName) {
-
- // clear table of previously loaded information
- $('[data--tbody-cardlisttablebody-bodydiv]').empty();
- $.ajax({
- type: "POST",
- url: "127.0.0.1/WebService.asmx/loadCardDetailsByCardName",
- data: '{cardName: "' + cardName + '"}',
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function (response) {
- var JSONresponse = JSON.stringify(response).substring(6, JSON.stringify(response).length - 2);
- // error check
- if (JSONresponse.substring(0, 5) == "Error") {
- } else if (JSONresponse == "") {
- // if no errors
- } else {
-
- JSONresponse = JSONresponse.replace(/\\/g, '');
- var jsonObject = $.parseJSON(JSONresponse);
- // display information into table for user
- $(function () {
- $.each(jsonObject, function (i, obj) {
- // convert 'is foil' response from numerical to 'True/False' phrase
- var cardNameFormatted = cardName;
-
- if (obj.isFoil == "True") {
- cardNameFormatted = cardName + " (Foil)";
- };
- // create row, loading data-tags & table cells with the information
- var $tr = '<tr ';
-
- if ((obj.isFoil == "True") && (parseInt(obj.cardQuantity) > 0)) {
- $tr += 'class="foilInStock"';
- }
- else if (parseInt(obj.cardQuantity) == 0){
-
- $tr += 'class="outOfStock"';
-
- };
- $tr += '>' +
- // card Name
- '<td>' + cardNameFormatted + '</td>' +
- // card Set
- '<td style="text-align:center">' + htmlUnescape(obj.setName) + '</td>' +
- // card Quantity
- '<td>' + obj.cardQuantity + '</td></tr>';
-
-
- // display row, to table
-
- $('#div-cardsDisplayTableBody-bodyDiv').append($tr);
- }); // end $.each
- }); // end function
- }; // end if
- }, // end success
- failure: function (msg) {
- }, //end failure
- complete: function (jqXHR, textStatus) {
- } // end complete
- }); // end $.ajax
- };
- /////////////////////////////////////////////////////////////////////// B
- // [B] Event Listeners //////////////////////////////////////////////// B
- ////////////////////////////////////////////////////////////////////// B
- // [B.1] card name selected
- /*
- */
- $('#select-cardListSelector-leftNavDiv').on("click vclick", function () {
-
- // check that card selected isn't the same card already loaded
- cardNameSelected($('#select-cardListSelector-leftNavDiv').val());
-
- });
- /////////////////////////////////////////////////////////////////////// C
- // [C] Global ///////////////////////////////////////////////////////// C
- ////////////////////////////////////////////////////////////////////// C
- // [C.1] This function is for the delay of when a user is typing in the card name box before it ajax searches
- /*
- */
- var delay = (function () {
- var timer = 0;
- return function (callback, ms) {
- clearTimeout(timer);
- timer = setTimeout(callback, ms);
- };
- })();
- /////////////////////////////////////////////////////////////////////// D
- // [D] Document Ready////////////////////////////////////////////////// D
- ////////////////////////////////////////////////////////////////////// D
- $(document).ready(function () {
-
- // [D.1] card search text box, seeking keystrokes
- /*
- */
- $('#inputText-cardNameSearchTextBox-leftNavDiv').on('keyup', cardNameSearchTextbox_keyup);
- // [D.2] prevent "Enter" keystroke from submitting in text input
- /*
-
- */
- $("#inputText-cardNameSearchTextBox-leftNavDiv").bind("keypress", function (e) {
- if (e.keyCode == 13) {
- return false;
- }
- });
- }); //document.ready
- </script>
- </body>
- </html>