admin管理员组文章数量:1344519
So i have a page and upon click button, it opens up a nested field and i want to hide one of the div in the filed upon click but so far my function doesnt work.
my js file
$('body').on('click', '#main-applicant-btn', function() {
event.stopPropagation();
var prependData = $('.main-applicant').children().first().clone();
prependData.find("div.checkbox").replaceWith( $( "div.checkbox.hide" ));
var prependedData = $('.main-applicant').children().last();
var list = document.getElementsByClassName("main-applicant");
list.insertBefore(prependData, prependedData);
})
my applicant.html.erb file
<div class="form-group">
<%= f.link_to_add "", :applicant_mitments, :class => "glyphicon glyphicon-plus-sign pointer add", :id => "main-applicant-btn", :data => {:target => "#main-applicant-#{rand}"} %>
</label>
</div>
<span id="main-applicant-<%= rand %>" class="main-applicant">
<%= f.simple_fields_for :applicant_mitments do |applicant_mitment| %>
<%= render "existing_mitment_fields", {:f => applicant_mitment, :instalment => true, :type => 'existing_mitment_applicant'} %>
<% end %>
</span>
my nested field existing_mitment_fields.html.erb
<div class="form-group monthly-instalment-lender-checkbox">
<div class="col-sm-3" id="checkbox-existing-fields">
<div class="checkbox">
<label>
<span>Joint Name</span>
<%= f.input_field :joint_name, as: :boolean, class: "joint-name-mitment" %>
</label>
</div>
</div>
</div>
So upon click i want to the main-applicant-btn
and when it displays the nested_fields, i will hide the div.checkbox
but so far when i try this code, i get error .insertBefore() is not a function.
Anyone has any clues on how to append or force javascript to run code after it has loaded?
So i have a page and upon click button, it opens up a nested field and i want to hide one of the div in the filed upon click but so far my function doesnt work.
my js file
$('body').on('click', '#main-applicant-btn', function() {
event.stopPropagation();
var prependData = $('.main-applicant').children().first().clone();
prependData.find("div.checkbox").replaceWith( $( "div.checkbox.hide" ));
var prependedData = $('.main-applicant').children().last();
var list = document.getElementsByClassName("main-applicant");
list.insertBefore(prependData, prependedData);
})
my applicant.html.erb file
<div class="form-group">
<%= f.link_to_add "", :applicant_mitments, :class => "glyphicon glyphicon-plus-sign pointer add", :id => "main-applicant-btn", :data => {:target => "#main-applicant-#{rand}"} %>
</label>
</div>
<span id="main-applicant-<%= rand %>" class="main-applicant">
<%= f.simple_fields_for :applicant_mitments do |applicant_mitment| %>
<%= render "existing_mitment_fields", {:f => applicant_mitment, :instalment => true, :type => 'existing_mitment_applicant'} %>
<% end %>
</span>
my nested field existing_mitment_fields.html.erb
<div class="form-group monthly-instalment-lender-checkbox">
<div class="col-sm-3" id="checkbox-existing-fields">
<div class="checkbox">
<label>
<span>Joint Name</span>
<%= f.input_field :joint_name, as: :boolean, class: "joint-name-mitment" %>
</label>
</div>
</div>
</div>
So upon click i want to the main-applicant-btn
and when it displays the nested_fields, i will hide the div.checkbox
but so far when i try this code, i get error .insertBefore() is not a function.
Anyone has any clues on how to append or force javascript to run code after it has loaded?
Share Improve this question edited Oct 1, 2015 at 2:59 Kingsley Simon asked Sep 30, 2015 at 23:40 Kingsley SimonKingsley Simon 2,2107 gold badges44 silver badges89 bronze badges1 Answer
Reset to default 8insertBefore
is a Node method.
document.getElementsByClassName()
method returns an HTMLCollection.
You've got to iterate through this HTMLCollection or to pick one of the elements inside it to actually touch the nodes and use their method :
list[0].insertBefore(...,...)
Also, as mentioned by @nnnnnn, "the insertBefore()
method isn't expecting jQuery objects as arguments.".
So either you use plain javascript methods like document.querySelector('')
or you call the target of the jQuery object ($(...)[0]
)
本文标签: jqueryinsertBefore() is not a function javascriptStack Overflow
版权声明:本文标题:jquery - .insertBefore() is not a function javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743748493a2532206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论