gevans Posted July 30, 2009 Share Posted July 30, 2009 Hey guys, I've written a little javascript (using the jquery library) to do some simple text removes on input and textarea elements. The code for the textarea is as follow; var value_array = new Array(); $(document).ready(function() { $("textarea").focus(function() { if($(this).html() == value_array[$(this).attr('name')]) { $(this).html(''); } }); $("textarea").blur(function() { if($(this).html() == '') { $(this).html(value_array[$(this).attr('name')]); } }); }); All it does is on page ready, assign the innerHtml value of all textarea to an array, indexed by their name attribute. focusing on the textarea will remove the text if it is the same as the stored text. blur will put the original text back in place if innerHtml is empty, otherwise it leaves the new content. This all works wonderfully appart from in safari where onblur it always puts the original text back in place. Any idea? Cheers Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 30, 2009 Share Posted July 30, 2009 instead of $(this).html() try $(this).val() Quote Link to comment Share on other sites More sharing options...
gevans Posted July 31, 2009 Author Share Posted July 31, 2009 I already use .val() for the input fields. It doesn't work with textareas as it doesnm't actually set a value originally, it's just considered innerHtml. Quote Link to comment Share on other sites More sharing options...
gevans Posted July 31, 2009 Author Share Posted July 31, 2009 Just thought, it should work with taking the innerHtml at the beggining, and doing the checks against the val()... $("textarea").focus(function() { if($(this).val() == value_array[$(this).attr('name')]) { $(this).html(''); }});$("textarea").blur(function() { if($(this).val() == '') { $(this).html(value_array[$(this).attr('name')]); }}); 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.