i am following Ryan Bates episode on nested form fields and have added the bit of jquery suggested at the end of part 2. Everything works well(i am able to add fields and remove fields). i now want to limit the number of fields you can add in the form. in my application.js i have
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
as Ryan Bates has written. reading another post i changed the lines to now read:
function add_fields(link, association, content) {
if($(".fields input").length < 5) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
}
However this does not work, am i doing something wrong here. thanks for the help.
*EDIT
This is the form
<%= form_for @question, :url => { :controller => "questions", :action => "create" } do |f| %>
<%= f.label(:name, "Request Question:") %>
<%= f.text_field(:name, :size => 72, :maxlength => 120, :id => "name") %><br />
<fieldset>
<legend><b>Tags</b></legend>
<%= f.fields_for :tags, :url => { :controller => "tags", :action => "create" } do |builder| %>
<%= render "tag_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add new keyword", f, :tags %></p>
</fieldset>
<% end %>
The tag field partial
<p class="fields">
<%= f.label(:keyword, "Keywords:") %>
<%= f.text_field(:keyword, :size => 20, :maxlength => 25, :id => "keyword") %>
<%= link_to_remove_fields "remove", f %>
</p>