0

I have a question. I build example for you and now i will write what I want to do. I want to set cursor inside input file only when i click a link "simple". As you can see, function set_curso_in_files() have conditional exacution. I take condition from mysql database. When is true - execute function.

This is not working fine. You can test. For example script set the cursor inside the input filed but after reload page this disappear. I thougt that .ready help, but not. I tested also function : event.preventDefault() but i not working fine when i click on the other link and come back.

What should i write

function set_cursor_in_filed() {
  $('#surname').focus();
}; // end set_cursor_in_filed

$(document).ready(function() {
  $('#gen_fak').click(function(event) {
    $.ajax({
      type: "GET",
      url: "do_exist.php",
      contentType: "application/json; charset=utf-8",
      dataType: 'json',
      success: function(json) {
        var do_exist = 0;
        for (var key in json) {
          var row = json[key];
          do_exist = row[0];
          if (row[0] == 0) {
            set_cursor_in_filed();
            alert("Condition true");
          } else {
            alert("Condition false");
          }
        }
      },
      error: function(blad) {
        alert("Error");
        console.log(blad);
      }

    }); // end ajax

  }); // end onclick
});
<html>

<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
</head>

<body>
  <p><a href="simple1.html"> simple1<a></p><br>
<p><a href="simple.html" id="gen_fak"> simple</a></p><br>
  <input type="text" id="surname">

</body>

</html>
Barmar
  • 741,623
  • 53
  • 500
  • 612
luki
  • 197
  • 11
  • please, fix the code indentation. It is painful to look at. – Igor Mar 26 '18 at 20:27
  • You need to use `event.preventDefault()` in the click handler, otherwise it will follow the link and this page will be gone. – Barmar Mar 26 '18 at 20:45
  • You also shouldn't alert "Condition false" for each non-matching element of the array. See https://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882 – Barmar Mar 26 '18 at 20:46
  • I deleted all alerts and i used event.preventDefault(), but still I have problem. Sorry for my code, but i do not know how should look like. I tested with event.preventDefault() writted in next line after $('#gen_fak').click(function(event) and it is working like that : when I click on link simple1.html and next i come back to link simple.html, cursor is not iniside input filed (no reaction on .click?), but after second click is inside input filed. that means , that it works only after second click on link widh id="gen_fak". Can somebody tell me why? – luki Mar 27 '18 at 15:44

0 Answers0