LoneStarJack Posted December 19, 2011 Share Posted December 19, 2011 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(); }; ?> Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 19, 2011 Share Posted December 19, 2011 css('display', 'none') Quote Link to comment Share on other sites More sharing options...
LoneStarJack Posted December 19, 2011 Author Share Posted December 19, 2011 css('display', 'none') works and so does the visibility fnction, but they will not turn back on with the $('#ta').onkeypress(function(). Quote Link to comment Share on other sites More sharing options...
haku Posted December 19, 2011 Share Posted December 19, 2011 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(). Quote Link to comment Share on other sites More sharing options...
jotorres1 Posted December 19, 2011 Share Posted December 19, 2011 I had a similar issue once, and all I had to add was <script language="javascript"> Try it out, see if that helps you. Quote Link to comment Share on other sites More sharing options...
haku Posted December 19, 2011 Share Posted December 19, 2011 That would be <script type="text/javascript">. 'language' has been long deprecated. Quote Link to comment Share on other sites More sharing options...
LoneStarJack Posted December 19, 2011 Author Share Posted December 19, 2011 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'); }; Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 Working example here: http://jsfiddle.net/8dG9N/ Quote Link to comment Share on other sites More sharing options...
LoneStarJack Posted December 19, 2011 Author Share Posted December 19, 2011 Thanks scootstah. Works great. I like your format for your answer. Now I want to work on the contents of the textarea having a length > 10 Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 What do you mean? Quote Link to comment Share on other sites More sharing options...
LoneStarJack Posted December 19, 2011 Author Share Posted December 19, 2011 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> Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 20, 2011 Share Posted December 20, 2011 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> Quote Link to comment Share on other sites More sharing options...
LoneStarJack Posted December 20, 2011 Author Share Posted December 20, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.