Home > Mobile >  Parametrize on() function jquery
Parametrize on() function jquery

Time:11-04

I have different user inputs. When one of them is changed I want to capture a change event only to its value. I'm trying to customize the code of an existing product. My goal is to pop up an error div when user input is wrong but my current code adds the error div under all inputs. This code works on all inputs but I want that it works only on input has title of label parent that contains 'Valore'.

JS CODE:

$(document).on('focusin', 'input', function () {
   $(this).data("oldvalue", this.value);
});
$(document).on('change', 'input', function () {
   var $formGroup = $(this).closest('.binf-form-group');
   if ($.isNumeric(this.value)) {
       $formGroup.find('.binf-help-block').remove();
   } else {
       this.value = $(this).data("oldvalue");
       $('<div  role="alert" style="white-space: normal;margin-top: 8px;background: #fff;color: #df3324;font-size: 11px;line-height: 16px;border-radius: 2px;border-left: 2px solid #df3324;padding: 0 8px;box-shadow: 0 0 4px 0 rgba(0,0,0,.3);">This value must be a number.</div>').appendTo($formGroup).fadeOut(3000, function () { $(this).remove()});
   }
});

HTML:

<div class="csui-field-text alpaca-container-item" data-alpaca-container-item-index="6" data-alpaca-container-item-name="4333_35" data-alpaca-container-item-parent-field-id="alpaca44">
    <div class="binf-form-group alpaca-field alpaca-field-text alpaca-required binf-has-error alpaca-invalid" data-alpaca-field-id="alpaca58">
        <label class="binf-control-label alpaca-control-label binf-col-sm-3" for="alpaca58" id="alpaca58Label" title="Valore">
           <span class="alpaca-icon-required binf-glyphicon binf-glyphicon-star"></span>
           Valore
        </label>
       <div class="binf-col-sm-9">
           <div class="alpaca-control">
               <div id="alpaca583195" class="cs-formfield cs-textfield cs-formfield-invalid">
                   <div class="cs-field-write">
                       <div class="cs-field-write-inner">
                           <input type="text" id="alpaca58" maxlength="32" placeholder="Add text" value="" aria-labelledby="alpaca58Label" aria-required="true" tabindex="-1">
                       </div>
                   </div>
               </div>
           </div>
        </div>
    </div>
</div>

Thanks for your help!

CodePudding user response:

$(document).on('change', 'input', function () {
   var title = $(this).closest(".binf-form-group").find("label").attr("title");
   if(title.indexOf("Valore") >= 0){
     var $formGroup = $(this).closest('.binf-form-group');
     if ($.isNumeric(this.value)) {
         $formGroup.find('.binf-help-block').remove();
     } else {
         this.value = $(this).data("oldvalue");
         $('<div  role="alert" style="white-space: normal;margin-top: 8px;background: #fff;color: #df3324;font-size: 11px;line-height: 16px;border-radius: 2px;border-left: 2px solid #df3324;padding: 0 8px;box-shadow: 0 0 4px 0 rgba(0,0,0,.3);">This value must be a number.</div>').appendTo($formGroup).fadeOut(3000, function () { $(this).remove()});
     }
   }
});
  • Related