Murali N
Murali N

Reputation: 3498

index() alternative in jquery 1.3.2

I have a very simple table structure

<table width='50%' align='center' id='tabs'>
  <tr>
    <td>1.00</td>
    <td>5.23</td>
    <td>6.12</td>
  </tr>
  <tr>
    <td>2</td>
    <td>2.45</td>
    <td>2.45</td>
  </tr>
  <tr>
    <td>3.12</td>
    <td>2.98</td>
    <td>2.09</td>
  </tr>
</table>

problem with index() function which doesn't work in jquery 1.3.2 can u please let me know any alternative to index() in jquery 1.3.2, this is the code if($(this).parent('td').index() % 2 === 0){ where i am getting the problem which returns always -1

$("table td").click( function(e){   
    if($(this).find('input').length){
         return ;   
    }        
    var input = $("<input type='text' size='5' />").val( $(this).text() );
    $(this).empty().append(input);
    $(this).find('input')
    .focus(function(e){
        if($(this).val()=='0.00' || $(this).val()=='0'){$(this).val('');}
    }).keydown(function(event){
        if($(this).parent('td').index() % 2 === 0){
         if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 190  || event.keyCode == 13 || 
              // Allow: Ctrl+A
             (event.keyCode == 65 && event.ctrlKey === true) || 
             // Allow: home, end, left, right
             (event.keyCode >= 35 && event.keyCode <= 39)) {
             // let it happen, don't do anything
             return;
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }   } }
    }).blur( function( e ){
                if($(this).val()!=""){
                    if (!isNaN(parseFloat($(this).val()))) {
                        var val1=parseFloat($(this).val()).toFixed(2);
                        $(this).val(val1);
                        $(this).parent('td').text( 
                              $(this).val()
                        );
                      }
                    }
                    else{
                            $(this).parent('td').text("0.00");
                    }
            });    
});

 $(function() {
        $('table tr td').hover(function() {
            $(this).css('background-color', '#FFFFB0');
        },
        function() {
            $(this).css('background-color', '#F4F4F4');
        });
    });

Please see JS FIDDLE HERE

Upvotes: 2

Views: 688

Answers (3)

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

If you must stick with v1.3.2, then you are lucky in that the :even selector will do the job, and was available from v1.0 .

You can also make the code considerably more readable, as follows :

...keydown(function(event) {
        var okKeys = [8,9,13,27,35,36,37,38,39,46,190],
            k = event.keyCode;
        if($(this).closest('td').is(":even")) {
            if ( $.inArray(k, okKeys) > -1 || (event.ctrlKey && k == 65) ) {
                return;// let it happen, don't do anything
            }
            else {
                // Ensure that it is a number and stop the keypress
                if (event.shiftKey || (k < 48 || k > 57) && (k < 96 || k > 105 )) {
                    event.preventDefault(); 
                }
            }
        }
    })...

$.inArray was available from v1.2 .

Upvotes: 1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

jQuery 1.3.2 supports prevAll(), so the form of index() you are using can be emulated with:

if ($(this).parent("td").prevAll().length % 2 === 0) {
    // ...
}

(As others have said, consider upgrading if at all possible. Version 1.3.2 is more than four years old now.)

Upvotes: 3

user1697238
user1697238

Reputation:

i think its help for you

$("table tr").find('td').each(function(index){
      // click td 
      $(this).click(function(){
        alert("index : " + index);
      });

}); 

Upvotes: 0

Related Questions