0

How to set jQuery custom attribute value as Integer instead of String. I would like to set the tabindex value dynamically using jQuery.

<input tabindex=1 /> 
var allInputs = new Array();
                        $(':input').each(
                                function() {
                                    allInputs.push($(this));
                                }
                            );

                        $.each(allInputs, function (i) {
                            var indexNumber = i + 1;
                            $(this).attr('tabindex', indexNumber );
                        });

This code sets an value as a String within "".

 e.g <input tabindex="1" />

Is there any other ways to set the value as an Integer so It will be

<input tabindex=1 />

/Jivan

  • you want to set tabindex value as string To int? – Pratik Joshi Mar 26 '14 at 12:27
  • 1
    Elements' attributes are always string. You can't set them as numbers. You can use them as numbers in your script or whereever you like by type casting them, however. – lshettyl Mar 26 '14 at 12:32

1 Answers1

4

In HTML, attribute value is always a string.

Even if you omit quotes, it will be assumed that they are there.

<input tabindex=1 />

is strictly exact the same thing that

<input tabindex="1" />

On rendering, the browser knows that tabindex string value is a number and do the parse internally. If it's not a number, browser will ignore it.

In fact, it's recommended using always quotes. That's the why when even set attribute value with integer (paseInt or not), the browser will wrap it into the quotes.. (or even jQuery wrapping explicitly into quotes)

Don't worry about the quotes, keep coding and be happy :)

Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74