tags:

views:

50

answers:

1

Hi,

I was trying to iterate through a collection of objects and was trying to set an attribute for each of the object.

Here is the code:

$(document).ready(function() 
{
    $('#clickButton').click(function() 
    {
        var hiddenVal = $('#hdnVal').val();

        $('*').find('*[tabindex]').each(function(index) 
        {
            //this.setAttribute('tabindex', hiddenVal + this.getAttribute('tabindex'));
            $(this).attr('tabindex', 'test');
        });
    });
});

I could not set the attribute with $(this).attr('', ''); but the JavaScript way works fine. How can I do it in jQuery?

+1  A: 

Setting a string to tabIndex will not work, it must be an integer.

$(this).attr('tabindex', 'test'); 
alert($(this).attr('tabindex')); 
// ^ alerts 0 in IE for me, indicating the default is restored

Try a number:

$(this).attr('tabindex', 1);
alert($(this).attr('tabindex')); 
// ^ alerts 1
Andy E
Actually I wanted to get a hidden value set in hiddenVal and prefix the current value with the existing one. When i checked in firebug, the value won't change.. is that because the values are considered as text?
Amit
@Amit: Possibly. Try casting the result to a number, eg `$(this).attr('tabindex', +(hiddenVal + $(this).attr('tabindex')));`
Andy E
Hey there were so many cross browser issues.. i did it with plain javascript.. anyway Thanks Andy for suggestions..
Amit