Jump to content

Display submit button after input in a TEXTAREA


LoneStarJack

Recommended Posts

I can't get the desired results with this code

 

<script>

$(document).ready(function(){

    $('#btn').css('visibility','hidden');

  });

$('#ta').onkeypress(function(){

    if($(this).val != ''){

      $('#btn').css('visibility','visible');

    };

    });

</script>

</head>

 

<?php

 

if (!isset($_POST["sql"])) {

echo '<body onload="document.forms[0].elements[0].focus()">';

echo '<div id="formx" >';

echo "<h2>Dump Query Records</h2>";

echo '<form action="dump_query_records.php" method="post">';

echo 'Cut and paste sql statement' . '<br>';

echo 'SQL : ';

echo '<textarea id="ta" cols="65" rows="6" ></textarea>';

echo "<br>";

echo '<input  ID="btn" type="submit"  value="Submit SQL!" >  </input>';

echo '</form>';

echo '</div>';

exit();

};

?>

That's because onkeypress() is a javascript event that is attached to HTML elements. But $("#ta") is a jquery object, not an HTML element. To attach an onkeypress event listener to the element referred to in a jquery object, you use .keypress(), not onkeypress().

I tried both suggestions and combinations thereof and it still was no go.

 

I am wondering about  #ta being a TEXTAREA.

 

$('#ta').keypress(function(){

    if($(this).val != ''){

      $('#btn').css('display', 'block');

    };

 

It was easier than I thought.

 

<script>

$(document).ready(function(){

    $('input#btn').hide();

$('textarea[id=text]').keypress(function(){

    if($(this).val().length > 10) {

        $('input#btn').show();

    } else {

        $('input#btn').hide();

    }

});

});

</script>

Wouldnt focus work better?

 

<script>
$(document).ready(function(){
    $('input#btn').hide();
      $('textarea[id="text"]').focus(function(){
          if($(this).val().length > 10) {
               $('input#btn').show();
          } else {
              $('input#btn').hide();
          }
   });
});
</script>

son.of.the.morning

The keypress, keyup, and keydown allow me to test the length of the field with each key stroke. If the length is greater than 10 the button will be shown. It will also hide the button should the count go below 10.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.