title: Why one way works and other doesn't?
body: <p>I have the following code:</p>
  <pre><code>if(chk) {
          document.getElementById(messagebox).innerHTML=&quot;update do be done&quot;;
          document.getElementById(messagebox).style.color=&quot;green&quot;; 
  
          } else {
              document.getElementById(messagebox).innerHTML=&quot;no update&quot;;
          document.getElementById(messagebox).style.color=&quot;red&quot;; 
          };  
  });
  </code></pre>
  <p>chk is a variable that comes from here:</p>
  <pre><code>var chk = ((lenta &gt;= 1) &amp;&amp; (lenta &lt;= 5) &amp;&amp; (str.search(/[0-9¬!\&quot;£\$%^\&amp;\*\(\)_\-+\=\{\}\[\]#~@\';\:\/\?&gt;\.&lt;\,\\\ |]/) &lt;= -1));
  </code></pre>
  <p>basically, if chk is true, it means that input is correct.
  In this case, input allows only letters and lenght between 1 and 5.</p>
  <p>script works fine but, if instead of &quot;</p>
  <pre><code>document.getElementById(messagebox).innerHTML=&quot;update do be done&quot;;
          document.getElementById(messagebox).style.color=&quot;green&quot;;
  </code></pre>
  <p>&quot; I add
  &quot;</p>
  <pre><code>$.ajax({
          type: 'POST',  
          data: ({quoteprefix : str}),
          url: 'liveupdate/firstsetup.php',
          success: function(data) {
              document.getElementById(messagebox).innerHTML=&quot; &lt;?php echo pw('updatedone'); ?&gt;. &quot;+data+&quot;&quot;;
              document.getElementById(messagebox).style.color=&quot;green&quot;;
          } 
      });
  &quot;
  </code></pre>
  <p>script updates no matter the inputted value
  Example: if I input AB it updates AB.
  If I input AB123 it updates &quot;AB&quot;</p>
  <p>the full script is here:</p>
  <pre><code>$(document).ready(function() {
  
  var ffind = &quot;#quoteprefix&quot;; //input value id
  $(ffind).on(&quot;change paste keyup&quot;, function() {
      var messagebox = &quot;qquoteprefix&quot;; //message box
      var lenta = $(this).val().length;
      var str = $(this).val();
  
      var chk = ((lenta &gt;= 1) &amp;&amp; (lenta &lt;= 5) &amp;&amp; (str.search(/[0-9¬!\&quot;£\$%^\&amp;\*\(\)_\-+\=\{\}\[\]#~@\';\:\/\?&gt;\.&lt;\,\\\ |]/) &lt;= -1));
      
      if (chk) {
          $(this).removeClass(&quot;is-invalid&quot;);
          $(this).addClass(&quot;is-valid&quot;);
      } else {
          $(this).removeClass(&quot;is-valid&quot;);
          $(this).addClass(&quot;is-invalid&quot;);
      };
      
  
  
  }); 
  
  $(ffind).on(&quot;focusout&quot;, function() {
      
  if(chk) {
          document.getElementById(messagebox).innerHTML=&quot;update de facut&quot;;
          document.getElementById(messagebox).style.color=&quot;green&quot;; 
  
          } else {
              document.getElementById(messagebox).innerHTML=&quot;nu face update&quot; +chk;
          document.getElementById(messagebox).style.color=&quot;red&quot;; 
          };  
  });
  
  
  
  
  });
  </code></pre>
  <p>UPDATE:
  tried this, same result:</p>
  <pre><code>$(document).ready(function() {
  
  var ffind = &quot;#quoteprefix&quot;; 
  $(ffind).on(&quot;change paste keyup&quot;, function() {
      var messagebox = &quot;qquoteprefix&quot;;
      var lenta = $(this).val().length;
      var str = $(this).val();
      var chk=false;
      var chk = ((lenta &gt;= 1) &amp;&amp; (lenta &lt;= 5) &amp;&amp; (str.search(/[0-9¬!\&quot;£\$%^\&amp;\*\(\)_\-+\=\{\}\[\]#~@\';\:\/\?&gt;\.&lt;\,\\\ |]/) &lt;= -1));
      
      if (chk) {
          $(this).removeClass(&quot;is-invalid&quot;);
          $(this).addClass(&quot;is-valid&quot;);
  
          $(this).focusout( function() {
              $.ajax({
                  type: 'POST', 
                  data: ({quoteprefix : $(this).val()}),
                  url: 'liveupdate/firstsetup.php',
                  success: function(data) {
                      document.getElementById(messagebox).innerHTML=&quot; &lt;?php echo pw('updatedone'); ?&gt;.&quot;+chk;
                      document.getElementById(messagebox).style.color=&quot;green&quot;;
                  } 
              });
          });
  
      } else {
          $(this).removeClass(&quot;is-valid&quot;);
          $(this).addClass(&quot;is-invalid&quot;);
      };
      
  
  }); 
  
  
  });
  </code></pre>
  <p>Why?
  Can someone help me</p>