# $(link).previous("input\[type=hidden\]") is undefined

**URL:** <https://www.ruby-forum.com/t/link-previous-input-type-hidden-is-undefined/186788>\
**Category:** Rails\
**Created:** [April 12, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/link-previous-input-type-hidden-is-undefined/186788 "2010-04-12T12:23:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 12, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/link-previous-input-type-hidden-is-undefined/186788/1 "2010-04-12T12:23:55Z")

</div>

I am getting this error when trying to use the remove link explained  
in the Railscast:

> **[\#197 Nested Model Form Part 2 - RailsCasts](http://railscasts.com/episodes/197-nested-model-form-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

error:  
$(link).previous(“input[type=hidden]”) is undefined  
remove\_fields()applic…1065175 (line 6)  
function onclick(event) { remove\_fields(this); return false; }()1  
(line 2)  
[Break on this error] $(link).previous(“input[type=hidden]”).value =  
“1”;

in the view file I have :

```
    <% echantillon_form.fields_for :treatments do |builder| %>
       <%= render "treatment", :f => builder %>
    <% end %>

```

in the partial I have :

```
     <tr>
     <td class ="name"> type d'examen: </td><td></td><td><%=

```

f.text\_field :treatmenttype %\>

```
     <tr>
     <td class ="name"> commentaires sur la preparation : </

```

td\>\<%= f.text\_field :preparation %\>

```
     <%= f.hidden_field :_destroy %>

   <tr><td><%= link_to_function "remove", "remove_fields(this)"

```

%\>

and in the appliction.js file I have:

function remove\_fields(link) {

$(link).previous(“input[type=hidden]”).value = “1”;  
$(link).up(“.treatmentfields”).hide();

}

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 12, 2010, 1:24pm UTC](https://www.ruby-forum.com/t/link-previous-input-type-hidden-is-undefined/186788/2 "2010-04-12T13:24:12Z")

</div>

On Apr 12, 11:22 am, ES [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> function remove\_fields(link) {
> 
> $(link).previous(“input[type=hidden]”).value = “1”;  
> $(link).up(“.treatmentfields”).hide();

Looks like because the hidden field and the link don’t have the same  
parent - the previous function finds you the previous sibling element  
(matching the selector if specified)

Fred

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 12, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/link-previous-input-type-hidden-is-undefined/186788/3 "2010-04-12T15:25:59Z")

</div>

I had to restructure my page in html so that the link did follow the  
hidden input field. It works now but when I follow the further  
instructions in the railscast to have an “add” link to add an object,  
I get this error:

undefined method `render\_partial’ for #\<ActionView::ReloadableTemplate:  
0x9f80d28\>

this is my view:

```
    <% echantillon_form.fields_for :treatments do |builder| %>
       <%= render "treatment", :f => builder %>
    <% end %>

  <%= link_to_add_fields "Add treatment",

```

echantillon\_form, :treatments %\>

On Apr 12, 1:22 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 12, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/link-previous-input-type-hidden-is-undefined/186788/4 "2010-04-12T15:44:08Z")

</div>

the problem was that I didn’t have “\_fields” as part of the name of my  
partial.
