[jQuery] Re: Passing a Index to a function
var li = $('ul li'); li.bind('click', function() { alert(li.index(this)); }); ...works for me. Also, if you are calling jQuery on the same set of objects more than once, it is wise to set a reference to it instead of re calling it more than once. Instead something such as this: $('ul li').bind('click', function() { alert($('ul li').index(this)); }); This makes your code faster - which is unnoticeable in this example, but when you have 2000 lines of it going on, you bet your sweet ass :) - and easier to change in the future. -Trey On Feb 10, 3:37 pm, Pedram pedram...@gmail.com wrote: I came back to my COde An I found your code fit in mine I am not going to use rowIndex because My Plugin it should work for every selector so what I atually want to do to get the selector name and bind an event to it and sent it to a function and also pass the index to the function and the reason I'm doing this is because I have some recursive functions in it and I badly needed this to be done . var RowSet={ close:false, eventClass:eventClass, eventType:click, ActionSelector:tr } function clickFunc(e){ console.log(e.data.Index); } $.fn.RowEffectByEvent=function(options){ RowSet = jQuery.extend({},RowSet,options); return this.each(function(){ oo=$(this); var k=(settings.header) ? :not(:first) : ; $(RowSet.ActionSelector+k,oo).each(function(index){ $(this).bind(RowSet.eventType,{Index:index},clickFunc); }); }); }; this is what I actually wanted to do thanks a lot guys you helped me alot and also RobG A appreciate it . thanks for the Tutorial also. On Feb 9, 1:26 pm, Ricardo Tomasi ricardob...@gmail.com wrote: $(table tr).each(function( index ){ $(this).click(function(){ alert('I am TR number ' + index); }); }); the index() function works on a collection of elements, so you'd have to get them first: $('table tr').click(function(){ alert( $(this).parent().children().index(this) ); }); On Feb 9, 5:48 pm, Pedram pedram...@gmail.com wrote: How COme we could not have access inside the BIND with THIS !! I'm Confused On Feb 9, 7:16 am, Pedram pedram...@gmail.com wrote: I check these two solutions it didn't work ... On Feb 8, 11:42 pm, RobG rg...@iinet.net.au wrote: On Feb 9, 4:54 pm, Pedram pedram...@gmail.com wrote: Dear folk, I want to get the index of the TR and send it to the Function , I don't know how to it . this is what I'm trying to do $(table tr).bind(click,{IndexName:$(this).index(this)},clickFunc) The DOM 2 HTML TR.rowIndex property should do the job. -- Rob
[jQuery] Re: Passing a Index to a function
I check these two solutions it didn't work ... On Feb 8, 11:42 pm, RobG rg...@iinet.net.au wrote: On Feb 9, 4:54 pm, Pedram pedram...@gmail.com wrote: Dear folk, I want to get the index of the TR and send it to the Function , I don't know how to it . this is what I'm trying to do $(table tr).bind(click,{IndexName:$(this).index(this)},clickFunc) The DOM 2 HTML TR.rowIndex property should do the job. -- Rob
[jQuery] Re: Passing a Index to a function
How COme we could not have access inside the BIND with THIS !! I'm Confused On Feb 9, 7:16 am, Pedram pedram...@gmail.com wrote: I check these two solutions it didn't work ... On Feb 8, 11:42 pm, RobG rg...@iinet.net.au wrote: On Feb 9, 4:54 pm, Pedram pedram...@gmail.com wrote: Dear folk, I want to get the index of the TR and send it to the Function , I don't know how to it . this is what I'm trying to do $(table tr).bind(click,{IndexName:$(this).index(this)},clickFunc) The DOM 2 HTML TR.rowIndex property should do the job. -- Rob
[jQuery] Re: Passing a Index to a function
$(table tr).each(function( index ){ $(this).click(function(){ alert('I am TR number ' + index); }); }); the index() function works on a collection of elements, so you'd have to get them first: $('table tr').click(function(){ alert( $(this).parent().children().index(this) ); }); On Feb 9, 5:48 pm, Pedram pedram...@gmail.com wrote: How COme we could not have access inside the BIND with THIS !! I'm Confused On Feb 9, 7:16 am, Pedram pedram...@gmail.com wrote: I check these two solutions it didn't work ... On Feb 8, 11:42 pm, RobG rg...@iinet.net.au wrote: On Feb 9, 4:54 pm, Pedram pedram...@gmail.com wrote: Dear folk, I want to get the index of the TR and send it to the Function , I don't know how to it . this is what I'm trying to do $(table tr).bind(click,{IndexName:$(this).index(this)},clickFunc) The DOM 2 HTML TR.rowIndex property should do the job. -- Rob
[jQuery] Re: Passing a Index to a function
On Feb 10, 5:48 am, Pedram pedram...@gmail.com wrote: How COme we could not have access inside the BIND with THIS !! I'm Confused I don't know why you want to make it so complex. Consider: $(table tr).click(function(){alert(this.rowIndex);}); If you want to call a function: $(table tr).click(function(){clickFunc(this.rowIndex)}); If you want to use bind (and I don't see the point, but anyhow...) $(table tr).bind(click, function() { clickFunc(this.rowIndex); }); function clickFunc(rowIndex) { alert(rowIndex); } or, to get closer to your original: $(table tr).bind(click, {prop1:'property 1', prop2: 'property 2'}, clickFunc); and in clickFunc: function clickFunc(e) { alert(this.rowIndex); } Going further: $(table tr).bind(click, {prop1:'property 1', prop2: 'property 2'}, clickFunc); function clickFunc(e) { alert(this.rowIndex + '\n' + e.data.prop1); } See? No selector needed, the row has a rowIndex property that tells you which row it is in the table. If you have a thead element, its rows are counted in the rowIndex and rows in tfoot include all the rows above (i.e. it is the index in the entire table). -- Rob
[jQuery] Re: Passing a Index to a function
thanks ROb , I Appreciate it , I'm writing a Plugin I am going to use the last part of your code , that I'm fine with that. great. THanks other guys , my Problem Is solved On Feb 9, 3:44 pm, RobG rg...@iinet.net.au wrote: On Feb 10, 5:48 am, Pedram pedram...@gmail.com wrote: How COme we could not have access inside the BIND with THIS !! I'm Confused I don't know why you want to make it so complex. Consider: $(table tr).click(function(){alert(this.rowIndex);}); If you want to call a function: $(table tr).click(function(){clickFunc(this.rowIndex)}); If you want to use bind (and I don't see the point, but anyhow...) $(table tr).bind(click, function() { clickFunc(this.rowIndex); }); function clickFunc(rowIndex) { alert(rowIndex); } or, to get closer to your original: $(table tr).bind(click, {prop1:'property 1', prop2: 'property 2'}, clickFunc); and in clickFunc: function clickFunc(e) { alert(this.rowIndex); } Going further: $(table tr).bind(click, {prop1:'property 1', prop2: 'property 2'}, clickFunc); function clickFunc(e) { alert(this.rowIndex + '\n' + e.data.prop1); } See? No selector needed, the row has a rowIndex property that tells you which row it is in the table. If you have a thead element, its rows are counted in the rowIndex and rows in tfoot include all the rows above (i.e. it is the index in the entire table). -- Rob
[jQuery] Re: Passing a Index to a function
I came back to my COde An I found your code fit in mine I am not going to use rowIndex because My Plugin it should work for every selector so what I atually want to do to get the selector name and bind an event to it and sent it to a function and also pass the index to the function and the reason I'm doing this is because I have some recursive functions in it and I badly needed this to be done . var RowSet={ close:false, eventClass:eventClass, eventType:click, ActionSelector:tr } function clickFunc(e){ console.log(e.data.Index); } $.fn.RowEffectByEvent=function(options){ RowSet = jQuery.extend({},RowSet,options); return this.each(function(){ oo=$(this); var k=(settings.header) ? :not(:first) : ; $(RowSet.ActionSelector+k,oo).each(function(index){ $(this).bind(RowSet.eventType,{Index:index},clickFunc); }); }); }; this is what I actually wanted to do thanks a lot guys you helped me alot and also RobG A appreciate it . thanks for the Tutorial also. On Feb 9, 1:26 pm, Ricardo Tomasi ricardob...@gmail.com wrote: $(table tr).each(function( index ){ $(this).click(function(){ alert('I am TR number ' + index); }); }); the index() function works on a collection of elements, so you'd have to get them first: $('table tr').click(function(){ alert( $(this).parent().children().index(this) ); }); On Feb 9, 5:48 pm, Pedram pedram...@gmail.com wrote: How COme we could not have access inside the BIND with THIS !! I'm Confused On Feb 9, 7:16 am, Pedram pedram...@gmail.com wrote: I check these two solutions it didn't work ... On Feb 8, 11:42 pm, RobG rg...@iinet.net.au wrote: On Feb 9, 4:54 pm, Pedram pedram...@gmail.com wrote: Dear folk, I want to get the index of the TR and send it to the Function , I don't know how to it . this is what I'm trying to do $(table tr).bind(click,{IndexName:$(this).index(this)},clickFunc) The DOM 2 HTML TR.rowIndex property should do the job. -- Rob
[jQuery] Re: Passing a Index to a function
$(table tr).click(function() { clickFunc($('table tr').index(this); }); On Feb 8, 10:54 pm, Pedram pedram...@gmail.com wrote: Dear folk, I want to get the index of the TR and send it to the Function , I don't know how to it . this is what I'm trying to do $(table tr).bind(click,{IndexName:$(this).index(this)},clickFunc) function clickFunc(event){ console.log(event.data.IndexName); }
[jQuery] Re: Passing a Index to a function
On Feb 9, 4:54 pm, Pedram pedram...@gmail.com wrote: Dear folk, I want to get the index of the TR and send it to the Function , I don't know how to it . this is what I'm trying to do $(table tr).bind(click,{IndexName:$(this).index(this)},clickFunc) The DOM 2 HTML TR.rowIndex property should do the job. -- Rob