adding text field if no previous, or previous is not empty

adding text field if no previous, or previous is not empty

Hello,

I have a form, where user can add skills, with text field.
When page load, there is just one field.

What I'd like to do is :

When that field get focus :
-  if there is no previous similar field OR if this previous field is not empty, AND if the current field is empty, add a new text field AFTER the current field.

So I don't need any "add new field" button, the script auto-add a new field, if the user is filling the last empty field.

I tried this way, but that doesn't work :
  1. function add_competence_field() {
        $(".je_competences").focus(function() {
            
            //alert("previous : " + $(this).prevAll().hasClass("je_competences").length);
            
            if( $(this).prevAll().hasClass("je_competences").length == 0 || $(this).prev().val() != "") {
            
                if($(this).val() == "") {
                    $(".je_competences:last").after( $(".je_competences:last").clone() );
                    add_competence_field();
                }
            
            }
            
        });
    }
    $(document).ready(function() {
        add_competence_field();
    });

















My form :

  1. <label for="je_competences">Domaines de compétences :</label>
                        <input type="text" name="je_competences" class="je_competences" id="je_competences" />
How Can I do this ?