Re: [jQuery] Parent and Single Parent Element Selection
On 3/5/07, Kevin Fricovsky [EMAIL PROTECTED] wrote: Morning, Evening ;) I have a question for the jquery group. My question is - what's the best way to get a single parent element of the current object. Right now I have an html table with multiple rows. In the first TD of each row I have a select list (a dropdown). I have a select() event attached to the option list and when the user selects an option the background color for that row (TR) is changed. (well, actually all TR backgrounds are changing right now that's why I'm writing everyone). So, the only problem I'm having is getting the single parent TR. Right now my update statement is updating every TR in the table versus just the parent. The code is something like this: $(../../../../tr,this).addClass(assigned); The this is the select element. Even if I do use an indexer on this statement like this $(...)[0] - the problem there is the system currently doesn't know the index of the row it's on. So I can either add the index in a hidden value or I thought maybe there's an easier way of doing this via JQuery. There is. $(this).parent(tr).addClass(assigned); -- Choan http://choangalvez.nom.es/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Parent and Single Parent Element Selection
Maybe something like: $('.colorPick').change(function() { $(this).parent().parent().addClass('assigned'); }); where colorPick is the class assigned to your select menu. You could also use: $('.colorPick').change(function() { $(this).parents('tr').addClass('assigned'); }); but my gut tells me that's not quite as efficient, as it will look for 'tr' elements all the way up to the top of the DOM tree. Seb On 5 Mar 2007, at 17:09, Kevin Fricovsky wrote: Morning, I have a question for the jquery group. My question is - what's the best way to get a single parent element of the current object. Right now I have an html table with multiple rows. In the first TD of each row I have a select list (a dropdown). I have a select() event attached to the option list and when the user selects an option the background color for that row (TR) is changed. (well, actually all TR backgrounds are changing right now that's why I'm writing everyone). So, the only problem I'm having is getting the single parent TR. Right now my update statement is updating every TR in the table versus just the parent. The code is something like this: $(../../../../tr,this).addClass(assigned); The this is the select element. Even if I do use an indexer on this statement like this $(...)[0] - the problem there is the system currently doesn't know the index of the row it's on. So I can either add the index in a hidden value or I thought maybe there's an easier way of doing this via JQuery. Thx for your help. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Parent Selection Problem
Guys, the codes work now ! Thanks a lot for the help ! Chris Karl Swedberg-2 wrote: Are you sure? Maybe the problem is with source.id I put your HTML into a page and ran this code (on document ready): $('input').click(function() { var parentId = $(this).parents('div.parent')[0].id; alert(parentId); }); When I clicked the first button, the alert read parent1 When I clicked the second, the alert read parent2 If that isn't what you're expecting, maybe I'm misunderstanding what you're trying to do. --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 15, 2007, at 12:02 PM, Mahadewa wrote: This doesn't do it for me. I got the Button itself as a result. var parent = $(this).parents('div.parent'); Is this not the same as to say: Select element (of 'this') which parent is 'div.parent' ? I think I need the other-way around, don't I ? Something like ... Get me the element, which class is 'parent', which has 'this' children. Chris Karl Swedberg-2 wrote: On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Hi Chris, you should be able to do it this way: var parent = $(this).parents('div.parent'); --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: Hi all, I've got the following problem ... Let's say I have the following html: div class=parent id=parent1 div class=sub input id=parent1_Button class=mybutton type=button value=button / /div /div div class=parent id=parent2 div class=sub input id=parent2_Button class=mybutton type=button value=button / /div /div Now given a JQuery object of any of the button, how can I select its parent div (the one with the class parent) ? I have tried the following, but it doesn't seem to yield the correct object: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Thanks a lot ! Chris -- View this message in context: http://www.nabble.com/Parent- Selection-Problem-tf3234532.html#a8987992 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/Parent- Selection-Problem-tf3234532.html#a8989357 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/Parent-Selection-Problem-tf3234532.html#a9001081 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
[jQuery] Parent Selection Problem
Hi all, I've got the following problem ... Let's say I have the following html: div class=parent id=parent1 div class=sub input id=parent1_Button class=mybutton type=button value=button / /div /div div class=parent id=parent2 div class=sub input id=parent2_Button class=mybutton type=button value=button / /div /div Now given a JQuery object of any of the button, how can I select its parent div (the one with the class parent) ? I have tried the following, but it doesn't seem to yield the correct object: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Thanks a lot ! Chris -- View this message in context: http://www.nabble.com/Parent-Selection-Problem-tf3234532.html#a8987992 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Parent Selection Problem
On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Hi Chris, you should be able to do it this way: var parent = $(this).parents('div.parent'); --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: Hi all, I've got the following problem ... Let's say I have the following html: div class=parent id=parent1 div class=sub input id=parent1_Button class=mybutton type=button value=button / /div /div div class=parent id=parent2 div class=sub input id=parent2_Button class=mybutton type=button value=button / /div /div Now given a JQuery object of any of the button, how can I select its parent div (the one with the class parent) ? I have tried the following, but it doesn't seem to yield the correct object: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Thanks a lot ! Chris -- View this message in context: http://www.nabble.com/Parent- Selection-Problem-tf3234532.html#a8987992 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Parent Selection Problem
This doesn't do it for me. I got the Button itself as a result. var parent = $(this).parents('div.parent'); Is this not the same as to say: Select element (of 'this') which parent is 'div.parent' ? I think I need the other-way around, don't I ? Something like ... Get me the element, which class is 'parent', which has 'this' children. Chris Karl Swedberg-2 wrote: On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Hi Chris, you should be able to do it this way: var parent = $(this).parents('div.parent'); --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: Hi all, I've got the following problem ... Let's say I have the following html: div class=parent id=parent1 div class=sub input id=parent1_Button class=mybutton type=button value=button / /div /div div class=parent id=parent2 div class=sub input id=parent2_Button class=mybutton type=button value=button / /div /div Now given a JQuery object of any of the button, how can I select its parent div (the one with the class parent) ? I have tried the following, but it doesn't seem to yield the correct object: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Thanks a lot ! Chris -- View this message in context: http://www.nabble.com/Parent- Selection-Problem-tf3234532.html#a8987992 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/Parent-Selection-Problem-tf3234532.html#a8989357 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Parent Selection Problem
Are you sure? Maybe the problem is with source.id I put your HTML into a page and ran this code (on document ready): $('input').click(function() { var parentId = $(this).parents('div.parent')[0].id; alert(parentId); }); When I clicked the first button, the alert read parent1 When I clicked the second, the alert read parent2 If that isn't what you're expecting, maybe I'm misunderstanding what you're trying to do. --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 15, 2007, at 12:02 PM, Mahadewa wrote: This doesn't do it for me. I got the Button itself as a result. var parent = $(this).parents('div.parent'); Is this not the same as to say: Select element (of 'this') which parent is 'div.parent' ? I think I need the other-way around, don't I ? Something like ... Get me the element, which class is 'parent', which has 'this' children. Chris Karl Swedberg-2 wrote: On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Hi Chris, you should be able to do it this way: var parent = $(this).parents('div.parent'); --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 15, 2007, at 11:01 AM, Mahadewa wrote: Hi all, I've got the following problem ... Let's say I have the following html: div class=parent id=parent1 div class=sub input id=parent1_Button class=mybutton type=button value=button / /div /div div class=parent id=parent2 div class=sub input id=parent2_Button class=mybutton type=button value=button / /div /div Now given a JQuery object of any of the button, how can I select its parent div (the one with the class parent) ? I have tried the following, but it doesn't seem to yield the correct object: // This function is wired to the above buttons' onclick event, passing 'this' as an argument function OnButtonClick(source) { var parent = $(.parent).children(source.id); } Thanks a lot ! Chris -- View this message in context: http://www.nabble.com/Parent- Selection-Problem-tf3234532.html#a8987992 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/Parent- Selection-Problem-tf3234532.html#a8989357 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Parent Selection Problem
On Feb 15, 2007, at 12:02 , Mahadewa wrote: var parent = $(this).parents('div.parent'); Is this not the same as to say: Select element (of 'this') which parent is 'div.parent' ? Nope, not the same. The way to read this is: - Wrap the DOM element this in a jQuery object. - Find all parents of the element that match div.parent. -- Jonathan Chaffer Technology Officer, Structure Interactive (616) 364-7423http://www.structureinteractive.com/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
[jQuery] parent()
I want to insert a row above an element.Imagine that I have:tabletrtdinput type=text id=myElement //td/tr/table I want to creat a new structure that looks like this:table tr class=errorCell colspan=100 tdspan class=errorPlease correct the error/spanbr //td /tr tr tdinput type=text id=myElement //td /tr /tableHowever, this produces no visual effect: $('#myElement').parent('tr').before('trtd class=errorCell colspan=100span class=errorPlease correct the error/spanbr //td/tr'); If I change the code, I see my new row: $('#myElement').parent().parent() .before('trtd class=errorCell colspan=100span class=errorPlease correct the error/spanbr //td/tr');I don't like this since it's bound to a specific table arrangement.Does anyone have any hints or different solutions?Thanks,Derrek[Long time lurker, first time poster :)] ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
However, this produces no visual effect: $('#myElement').parent('tr') .before('trtd class=errorCell colspan=100span class=errorPlease correct the error/spanbr //td/tr'); If I change the code, I see my new row: $('#myElement').parent().parent() .before('trtd class=errorCell colspan=100span class=errorPlease correct the error/spanbr //td/tr'); I don't like this since it's bound to a specific table arrangement. Does anyone have any hints or different solutions? What happens if you just do: $('#myElement').parent().before('tr.../tr'); It seems like something like that should work just fine. (The fact that .parent().parent() works kind of makes me suspicious) Do you have an example up anywhere of the problem in action? --John ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
But, to my mind, it makes sense to do $('#myElement').parent().parent() because the parent of the input is a td and the parent of the td is the tr. Or is that wrong? No, you're right - On first glance I thought it was td id=myElement ... /td sorry about that. Not sure why .parent('tr') didn't work, though. --John ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
Hey John,Unfortunately, I don't have an example up.But, to my mind, it makes sense to do $('#myElement').parent().parent() because the parent of the input is a td and the parent of the td is the tr. Or is that wrong? DerrekOn 10/18/06, John Resig [EMAIL PROTECTED] wrote: However, this produces no visual effect:$('#myElement').parent('tr') .before('trtd class=errorCell colspan=100span class=errorPlease correct the error/spanbr //td/tr'); If I change the code, I see my new row: $('#myElement').parent().parent().before('trtd class=errorCell colspan=100span class=errorPlease correct the error/spanbr //td/tr'); I don't like this since it's bound to a specific table arrangement. Does anyone have any hints or different solutions? What happens if you just do:$('#myElement').parent().before('tr.../tr');It seems like something like that should work just fine. (The factthat .parent().parent() works kind of makes me suspicious) Do you have an example up anywhere of the problem in action?--John___jQuery mailing listdiscuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
But, to my mind, it makes sense to do $('#myElement').parent().parent() because the parent of the input is a td and the parent of the td is the tr. Or is that wrong? No, you're right - On first glance I thought it was td id=myElement ... /td sorry about that. Not sure why .parent('tr') didn't work, though. Doesn't .parent() only look up one level? I thought you'd use .ancestors(). ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
I'm consfused about the difference between parents() and ancestors(). The documentation on visualjquery.com isn't very clear as to the difference and the behavior seems to be the same. On 10/18/06, Dave Methvin [EMAIL PROTECTED] wrote: But, to my mind, it makes sense to do $('#myElement').parent().parent() because the parent of the input is a td and the parent of the td is the tr.Or is that wrong? No, you're right - On first glance I thought it was td id=myElement... /td sorry about that. Not sure why .parent('tr') didn't work, though.Doesn't .parent() only look up one level? I thought you'd use .ancestors(). ___jQuery mailing listdiscuss@jquery.comhttp://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
Doesn't .parent() only look up one level? I thought you'd use .ancestors(). Oh yeah... man, I'm totally out of it today. Derrek - you should be using .parents(tr) or .ancestors(tr) (they're synonymous). --John ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
Yeah, but if I have a table embedded within a table (ugh, I know) then I find both the trs. I tried parents('tr').get(0), but that's not right either.Suggestions? On 10/18/06, John Resig [EMAIL PROTECTED] wrote: Doesn't .parent() only look up one level? I thought you'd use .ancestors().Oh yeah... man, I'm totally out of it today. Derrek - you should beusing .parents(tr) or .ancestors(tr) (they're synonymous). --John___jQuery mailing listdiscuss@jquery.comhttp://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
Yeah, but if I have a table embedded within a table (ugh, I know) then I find both the trs. I tried parents('tr').get(0), but that's not right either. The issue is that that the trs are returned in the order in which they're in the document. So the highest tr in the document is returned first. So the best way to do this is by doing: $(...).parents(tr:last) Which will give you the last tr found (but the one closest to your starting element). --John ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
Hi John,Say I have:table id=tbl_atr id=tr_xtd/td/trtr id=tr_ytd/td/trtr id=tr_ztd/td/tr tr id=tr_atd id=td_a table id=tbl_b tr id=tr_b td id=td_b input id=myInput type=text / /td /tr /table/td/tr/tableIf I do $('#myInput').parents('tr:last').before('tr ... /tr') Then the the new row appears between tr id=tr_z and tr id=tr_a and not between table id=tbl_b and tr id=tr_b like I would expect. Did I screw up something?On 10/18/06, John Resig [EMAIL PROTECTED] wrote: Yeah, but if I have a table embedded within a table (ugh, I know) then I find both the trs.I tried parents('tr').get(0), but that's not right either.The issue is that that the trs are returned in the order in whichthey're in the document. So the highest tr in the document isreturned first. So the best way to do this is by doing: $(...).parents(tr:last)Which will give you the last tr found (but the one closest to yourstarting element).--John___jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
Try :first. I think parents() actually works differently from the other filters, in that the elements are in reverse order (parent is [0], grandparent is [1]).BlairOn 10/19/06, Derrek [EMAIL PROTECTED] wrote: Hi John,Say I have:table id=tbl_atr id=tr_xtd/td/trtr id=tr_ytd/td/trtr id=tr_ztd/td/tr tr id=tr_atd id=td_a table id=tbl_b tr id=tr_b td id=td_b input id=myInput type=text / /td /tr /table/td/tr/tableIf I do $('#myInput').parents('tr:last').before('tr ... /tr') Then the the new row appears between tr id=tr_z and tr id=tr_a and not between table id=tbl_b and tr id=tr_b like I would expect. Did I screw up something?On 10/18/06, John Resig [EMAIL PROTECTED] wrote: Yeah, but if I have a table embedded within a table (ugh, I know) then I find both the trs.I tried parents('tr').get(0), but that's not right either.The issue is that that the trs are returned in the order in whichthey're in the document. So the highest tr in the document isreturned first. So the best way to do this is by doing: $(...).parents(tr:last)Which will give you the last tr found (but the one closest to yourstarting element).--John___jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___jQuery mailing listdiscuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent()
Hey Blair,That did the trick. Thanks for the help.But I must (regretfully) say that the documentation on this point is lacking.It seems to me that parents() and ancestors() are the same except for the order of objects returned. parents() = parent[0], grandparent[1]ancestors() = grandparent[0], parent[1]Perhaps the someone could add this simple explanation to the documentation.I appreciate everyone's help,Derrek On 10/18/06, Blair McKenzie [EMAIL PROTECTED] wrote: Try :first. I think parents() actually works differently from the other filters, in that the elements are in reverse order (parent is [0], grandparent is [1]).Blair On 10/19/06, Derrek [EMAIL PROTECTED] wrote: Hi John,Say I have:table id=tbl_atr id=tr_xtd/td/trtr id=tr_ytd/td/trtr id=tr_ztd/td/tr tr id=tr_atd id=td_a table id=tbl_b tr id=tr_b td id=td_b input id=myInput type=text / /td /tr /table/td/tr/tableIf I do $('#myInput').parents('tr:last').before('tr ... /tr') Then the the new row appears between tr id=tr_z and tr id=tr_a and not between table id=tbl_b and tr id=tr_b like I would expect. Did I screw up something?On 10/18/06, John Resig [EMAIL PROTECTED] wrote: Yeah, but if I have a table embedded within a table (ugh, I know) then I find both the trs.I tried parents('tr').get(0), but that's not right either.The issue is that that the trs are returned in the order in whichthey're in the document. So the highest tr in the document isreturned first. So the best way to do this is by doing: $(...).parents(tr:last)Which will give you the last tr found (but the one closest to yourstarting element).--John___jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___jQuery mailing listdiscuss@jquery.com http://jquery.com/discuss/ ___jQuery mailing listdiscuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
[jQuery] parent() modifies original object
I'm not sure if this is the way it's supposed to work, but this makes absolutely no sense to me as far as implementation goes. If I have code like this:function someFunction( e ){var cell = $(e);var cellParent = cell.parent();var cellParent2 = cell.parent();alert([ + cell.id() + ] [ + cellParent.id() + ] [ + cellParent2.id() + ]);}Instead of printing the equivalent of: $(e).id(), $(e).parent().id(), and $(e).parent().parent().id(), it prints $(e).parent.parent.id() three times. Further digging seems to hint at the fact that every time cell.parent() is called, it's the equivalent of cell = cell.parent() and as such the original value of cell is lost.Is this behavior intentional, or is it a bug? It seems quite counterintuitive to me, and while in the dumbed-down example above it makes more sense to use $(e).parent() instead of cell.parent(), I find it more intuitive to use cell.parent() in other instances and thus discovered this behavior.If it's unintentional, I'll start trying to find a fix and whip up some test cases too, I just wanted to check before I go fixing something that's not a bug in the first place. Cheers,~Peter ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent() modifies original object
Peter Woods schrieb: I'm not sure if this is the way it's supposed to work, but this makes absolutely no sense to me as far as implementation goes. If I have code like this: [...] Instead of printing the equivalent of: $(e).id(), $(e).parent().id(), and $(e).parent().parent().id(), it prints $(e).parent.parent.id() three times. Further digging seems to hint at the fact that every time cell.parent() is called, it's the equivalent of cell = cell.parent() and as such the original value of cell is lost. Is this behavior intentional, or is it a bug? It seems quite counterintuitive to me, and while in the dumbed-down example above it makes more sense to use $(e).parent() instead of cell.parent(), I find it more intuitive to use cell.parent() in other instances and thus discovered this behavior. It is intentional. parent() is a destructive operation on the jQuery object like filter(), find() etc. and can be reverted with end(). There is the concept to pass an additional function to all desctructive operations: That function is executed for the modified stack and then returns an unmodified object. Maybe the discussion should be started again... There were several votes that said it would be unintuitive if a single method is once destructive and once it is not because an additional function is passed... What do you think? -- Jörn ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent() modifies original object
I thought I'd read somewhere on here about functions being destructive, but I wasn't sure hence the question. While it makes sense when you're doing crazy chained method calls, I think it's very unintuitive during regular use of the parent() method, especially because there's nothing which signifies in the method call that it is indeed destructive (such as in Ruby or Scheme where you have set! or what not). It's also not noted in the API... which is definitely an oversight no matter how you look at it. Maybe someone can shed some light on specific situations where the destructive method saves considerable amounts of code. I can definitely see how it'd be useful--do some work on the parent, then end(), then do some work on the element itself--but is this functionality used frequently? I think it makes perfect sense for filter(), but even so, I think it should be made clear in some way or another that a function you're calling is destructive, otherwise it can create the incredibly confusing behavior I demonstrated above unless you're made explicitly aware that it's not doing what you think it's doing. Thanks for the answer nonetheless, and I apologize in advance to John and Jörn if this has been discussed to death before! Cheers! ~Peter On 10/17/06, Jörn Zaefferer [EMAIL PROTECTED] wrote: Peter Woods schrieb: I'm not sure if this is the way it's supposed to work, but this makes absolutely no sense to me as far as implementation goes. If I have code like this: [...] Instead of printing the equivalent of: $(e).id(), $(e).parent().id(), and $(e).parent().parent().id(), it prints $(e).parent.parent.id() three times. Further digging seems to hint at the fact that every time cell.parent() is called, it's the equivalent of cell = cell.parent() and as such the original value of cell is lost. Is this behavior intentional, or is it a bug? It seems quite counterintuitive to me, and while in the dumbed-down example above it makes more sense to use $(e).parent() instead of cell.parent(), I find it more intuitive to use cell.parent() in other instances and thus discovered this behavior. It is intentional. parent() is a destructive operation on the jQuery object like filter(), find() etc. and can be reverted with end(). There is the concept to pass an additional function to all desctructive operations: That function is executed for the modified stack and then returns an unmodified object. Maybe the discussion should be started again... There were several votes that said it would be unintuitive if a single method is once destructive and once it is not because an additional function is passed... What do you think? -- Jörn ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] parent() modifies original object
I think we are talking about two different effects here. This can definitely be subtle so it's good to have someone ask the question. var cell = $(e); var cellParent = cell.parent(); var cellParent2 = cell.parent(); ... Further digging seems to hint at the fact that every time cell.parent() is called, it's the equivalent of cell = cell.parent() and as such the original value of cell is lost. Most jQuery methods are designed to be chainable; they return the original object as the return value so that a subsequent method can be called on it. That is true of the .parent() method. So what's happening is that you have created three different aliases for the same object! The two .parent() method calls are modifying the internal state of the *same* jQuery object and returning that object back to you. It's the internal state of the jQuery object that remembers the selected nodes, not the return value. To create a brand new jQuery object that contains the parent, without changing the jQuery object with cell, you could use one of these: var cell = $(e); // Copy cell object and then get parent in the copied object var cellParent = $(cell).parent(); // XPath expr navigates to parent using cell as the context var cellParent2 = $(.., cell); The $(cell) usage for creating a copy of a jQuery object is described in the documentation for $(), but navigating the jQuery site I had a hard time finding the XPath documentation to explain the second example. Destructive methods like .parent() that change the jQuery object's selected nodes always push the previous list of selected nodes onto an internal stack that can be popped with the .end() method. So in the case above, the jQuery object stored in cellParent has a pushed copy of the DOM node selected by cell (the same DOM node originally passed in by e) that you can get to if you use .end(). The problem you originally mentioned above would have occurred whether destructive methods worked this way or not. It's just a difference of whether the object stacked the previous (two) sets of nodes. Maybe someone can shed some light on specific situations where the destructive method saves considerable amounts of code. I can definitely see how it'd be useful--do some work on the parent, then end(), then do some work on the element itself--but is this functionality used frequently? It's useful when you get comfortable with chaining because it avoids having to declare and use extra variables. I would actually prefer an explicit push/pop approach myself because the creation of the stacked nodes is too implicit and the stacked node references are cumbersome to eliminate if you don't want them. Sounds like this was (painfully!) reduced from some more complex code. Maybe there is a jQuery-ish way to do it? You can avoid trouble with aliases if you chain methods, since you aren't storing the object in a variable. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/