ruby on rails - jQuery .closest() returning multiple results -


i trying implement validation on html form highlight form inputs bootstrap class has-error in case user enter wrong informations in rails app. using ajax custom data object contains error messages. far i've come this:

application.js

   jquery.each(data.messages, function(i, val){     $("label").val(i).closest("form-group").addclass("has-error");   }); 

_account.html.erb

<div class="form-group">  <%= account_form.label(:label, "label") %>  <%= account_form.text_field(:label, class: "form-control") %> </div> <div class="form-group">  <%= account_form.label(:iban, "iban") %>  <%= account_form.text_field(:iban, class: "form-control", maxlength: 29, data: {toggle: "tooltip"}) %> </div> <div class="form-group">  <%= account_form.label(:swift, "swift") %>  <%= account_form.number_field(:swift, class: "form-control", maxlength: 8, data: {toggle: "tooltip"}) %> </div> 

for example, i = "swift" , val = "is blank", if user provides label forgets other two, "swift" , "iban" in i.

my problem each loop .each does, .closest returns 3 divs can't manage select single div based on label .closest

try this:-

 jquery.each(data.messages, function(i, val){   $("label:contains('"+i+"')").closest("form-group").addclass("has-error"); }); 

or match text:-

jquery.each(data.messages, function(i, val){   var lbl=$('label').filter(function () {        return $(this).text().tolowercase()==i.tolowercase();    }).first();   lbl.closest(".form-group").addclass("has-error"); }); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -