jquery set tabindex and cursor

24,016

Solution 1

Try :

$('#register1').find('input[tabindex=1]').whatyouwant()

Solution 2

Simply select the item with tabindex one in your loop using a condition:

$(function(){
    var tabindex = 1;
    $('#register1').find('input,select').each(function() {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);

            // select the first one.
            if (tabindex == 1) {
               $input.select();
            }
            tabindex++;
        }
    });
});
Share:
24,016

Related videos on Youtube

ShaneKm
Author by

ShaneKm

I am a .Net software engineer, mentor, thinker, and loud-mouth on the microservices, software architecture, and development of enterprise applications. With over 15+ years of commercial software development experience across a wide range of technologies, I’ve successfully delivered software products for embedded, Windows, and web platforms. I'm a passionate manager and developer always looking for opportunities and challenges with an approach to defining computing and network infrastructure through patterns, SOLID principles, and practices of writing clean and extensible code. In my free time, you can find me exploring the city, at the gym, or reading a book.

Updated on September 10, 2020

Comments

  • ShaneKm
    ShaneKm over 3 years

    I have the following code that assigns tabindex to my form id "register1". I would like to place the cursor on the first input or select list item on the form (item with tabindex = 1) once tabindexes are assigned. but the following line: $('#register1').find('input').attr('tabindex',1).select(); Resets tabindex of all the inputs.

    Full code:

    $(function(){
        var tabindex = 1;
        $('#register1').find('input,select').each(function() {
            if (this.type != "hidden") {
                var $input = $(this);
                $input.attr("tabindex", tabindex);
                tabindex++;
            }
        });
        $('#register1').find('input').attr('tabindex',1).select();
    });
    

    thanks