jquery insertAfter or append question

Posted by user271619 on Stack Overflow See other posts from Stack Overflow or by user271619
Published on 2010-05-26T23:42:37Z Indexed on 2010/05/27 0:11 UTC
Read the original article Hit count: 517

Filed under:
|

I am playing around with validating form inputs.

For now I'm simply having fun with displaying a message after the input. I'm using the after() method to display simple text, and when I purposely make a mistake in the box I display the message perfectly.

However, if I purposely make a mistake again, it immediately adds the same message after the first error message.

Is there a method that replaces the original? I didn't want to try to add some more code to look for that original error message, delete it, and then insert the same message again.

I will if I have to, but I thought I'd ask first.

Here's my code:

<script language="javascript" type="text/javascript">
$(document).ready(function(){

  $("form#testform").submit(function(){
     if($("#fieldname1").val().length < 5){ $("#fieldone").after("<div>not enough characters</div>"); }
  });return false;


});
</script>

<form id="testform" method="post">         
<table>
    <tr>
        <td style="width:100px;">Field One:</td>
        <td><input type="text" name="fieldname1" id="fieldname1" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
</table>
</form>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about insertafter