Eightleven Eightleven - 4 months ago 15
jQuery Question

Javascript can't get value if input field connected to database?

My javascript can't get input field if it's connected to database? if typed manually it's work. any trick for that?

Here's my html code:

<html>
<head>
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function () {
$('#show').load('data.php')
}, 2000);
});
</script>
<script>
$(function() {
var money = 7000;

$("#show").on("change keyup", function() {
var input = $(this);

// show message
if( input.val() > money )
$('#click').trigger('click');
});
});
</script>
</head>
<body>
<textarea type="text" id="show" value=""></textarea>

<button type="button" onclick="alert('Hello');" id="click">Click</button>
</body>
</html>


if value > my variable, then send trigger click. if typed manually it's work bro. any solution?

Jai Jai
Answer

You can trigger the click in the .load(url, [callback]) optional callback function:

Few other changes, you don't have to maintain two different doc ready blocks one is enough to do the work, So you can combine them as:

<script type="text/javascript">
  $(document).ready(function() {
    var money = 7000;
    setInterval(function() {
      $('#show').load('data.php', function() {
        if ($("#show").val() > money) {
          $('#click').trigger('click');
        }
      });
    }, 2000);

    $("#show").on("change keyup", function() {
      var input = $(this);
      // show message
      if (input.val() > money)
        $('#click').trigger('click');
    });
  });
</script>