简体   繁体   中英

Focus on next input element by refer class name using jquery

when user input value in text box it should move to next textbox if that input field contain specific class name but if another text box doesn't have that class name then it should not move to next

<input type="text" maxlength="1" class="txt_cls" />
<input type="text" maxlength="1" class="txt_cls" />
<input type="text" maxlength="1" class="txt_cls" />
<input type="text" maxlength="1" class="" />
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>

$(document).ready(function(){
    $('input.txt_cls').on("input", function(){
        if($(this).val().length==$(this).attr("maxlength")){

            // $(this).next().focus();

        $(this).next().find('.txt_cls').focus();
        }
    });
});
</script>

oh I did small mistake, forgot to mention .closest(); now it is working fine

$(document).ready(function(){
    $('input.txt_cls').on("input", function(){
        if($(this).val().length==$(this).attr("maxlength")){
        //$(this).next().focus();
         $(this).next().closest('.txt_cls').focus();
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM