# RJS : find the parent's id

**URL:** <https://www.ruby-forum.com/t/rjs-find-the-parents-id/145311>\
**Category:** Rails\
**Created:** [August 7, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/rjs-find-the-parents-id/145311 "2008-08-07T20:49:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joserwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@joserwan](https://www.ruby-forum.com/u/joserwan)\
**Post date:** [August 7, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/rjs-find-the-parents-id/145311/1 "2008-08-07T20:49:08Z")

</div>

Hi,

I have a HTML like this :

....

In my controller, I have a “update\_data” method, called every x  
seconds from a periodical\_call\_remote :

I want to take all the elements having the class “data\_1”, and replace  
their html with :

render :partial =\> data\_1, :local =\> {:user\_id =\> user\_id}, where  
user\_id is the id of the parent of p.data\_1

But I didn’t find how to to catch the parents id of p.data\_1 !

Does someone have an idea ?

Thank you

---

<div class="post-metadata">

**Author:** ![joserwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@joserwan](https://www.ruby-forum.com/u/joserwan)\
**Post date:** [August 8, 2008, 9:58am UTC](https://www.ruby-forum.com/t/rjs-find-the-parents-id/145311/2 "2008-08-08T09:58:57Z")

</div>

page[“data\_1”]

---

<div class="post-metadata">

**Author:** ![joserwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@joserwan](https://www.ruby-forum.com/u/joserwan)\
**Post date:** [August 8, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/rjs-find-the-parents-id/145311/3 "2008-08-08T14:22:11Z")

</div>

Thank you for your response but :

In this case, it will look for the element wich have the id “data\_1”.

And if I have 50 users, I’ll have 50 “data\_1” classes.

I would like a way to find the css equivalent of “#user\_1 .data\_1”

I tried :

page.select(“user\_1”).each{|elt|  
elt.select(“p.data\_1”).each{|u\_elt|  
page.replace\_html u\_elt, “test”  
}  
}

But it doesn’t work… ☹

---

<div class="post-metadata">

**Author:** ![joserwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@joserwan](https://www.ruby-forum.com/u/joserwan)\
**Post date:** [August 8, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/rjs-find-the-parents-id/145311/4 "2008-08-08T15:21:57Z")

</div>

On 8 Aug 2008, at 13:21, joserwan wrote:

> page.select(“user\_1”).each{|elt|  
> elt.select(“p.data\_1”).each{|u\_elt|  
> page.replace\_html u\_elt, “test”  
> }  
> }

I’m coming into this half way, but that selector you’ve got there is  
wrong (that would select tags called user\_1)

In pure prototype,

$(‘user\_1’).select(‘p.data\_1’).each(function(element){ …})

There are limits to what rjs can map to javascript, something like

page.select(’#user\_1 p.data\_1’).each {|elt| elt.update ‘test’}

would probably do it, but I’d probably stop fighting rjs and write it  
by hand.

Fred

---

<div class="post-metadata">

**Author:** ![joserwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@joserwan](https://www.ruby-forum.com/u/joserwan)\
**Post date:** [August 8, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/rjs-find-the-parents-id/145311/5 "2008-08-08T15:32:30Z")

</div>

Found it ! 😃

render :update do |page|  
page[‘user\_1’].down(‘p.data\_1’).replace\_html “test”  
end

It’s not the transcription of the prototype style you told me, but it  
works…

I haven’t tried, but  
$(‘user\_1’).select(‘p.data\_1’).each(function(element){ …})  
should be  
page[‘user\_1’].select(‘p.data\_1’)…

On Aug 8, 9:21 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
