Cleave.js: Why there is no jquery version?

Created on 19 Apr 2018  路  8Comments  路  Source: nosir/cleave.js

kind of (+autoUnmask option for numeric):

$('#price,#sum').cleave({ numeral: true, numeralThousandsGroupStyle: 'thousand', autoUnmask: true});
(function($, window, undefined){ 'use strict'

  $.fn.cleave = function(opts) {

    var defaults = {autoUnmask: false},
        options = $.extend(defaults, opts || {});

    return this.each(function(){

      var cleave = new Cleave('#'+this.id, options), $this = $(this);

      $this.data('cleave-auto-unmask', options['autoUnmask']);;
      $this.data('cleave',cleave)
    });
  }

  var origGetHook, origSetHook;

  if ($.valHooks.input) {

    origGetHook = $.valHooks.input.get;
    origSetHook = $.valHooks.input.set;

  } else {

    $.valHooks.input = {};
  }

  $.valHooks.input.get = function (el) {

    var $el = $(el), cleave = $el.data('cleave');

    if( cleave ) {

      return $el.data('cleave-auto-unmask') ? cleave.getRawValue() : el.value;

    } else if( origGetHook ) {

      return origGetHook(el);

    } else {

      return undefined;
    }
  }

  $.valHooks.input.set = function (el,val) {

    var $el = $(el), cleave = $el.data('cleave');

    if( cleave ) {

      cleave.setRawValue(val);
      return $el;

    } else if( origSetHook ) {

      return origSetHook(el);

    } else {
      return undefined;
    }
  }
})(jQuery, window);

Most helpful comment

The line "var cleave = new Cleave('#'+this.id, options), $this = $(this);" should be "var cleave = new Cleave(this, options), $this = $(this);" to be fully compatible, you are otherwise assuming that whatever was passed to the JQuery function has an ID which may or may not be true

All 8 comments

And how can i get getRawValue()
$(".my-input").getRawValue()

just use $(".my-input").val()

Nice, so I added a link in README, jQuery deserves more love than any other libs.
https://github.com/nosir/cleave.js#jquery-fn-usage

The line "var cleave = new Cleave('#'+this.id, options), $this = $(this);" should be "var cleave = new Cleave(this, options), $this = $(this);" to be fully compatible, you are otherwise assuming that whatever was passed to the JQuery function has an ID which may or may not be true

@Swizzy Thanks, you are right. I just don't remember why I needed 'id')

Any ideas on how to get .destroy() to work with this jQuery version?

Here's a fiddle of my attempt to get this to work.

My basic requirement is to be able to remove Cleave dynamically.

Any ideas on how to get .destroy() to work with this jQuery version?

Here's a fiddle of my attempt to get this to work.

My basic requirement is to be able to remove Cleave dynamically.

According to your jsfiddle code:

$('#btnRemoveCleave').on('click', function() {
    console.log('destroy');
    abc.data('cleave').destroy();
});

@armenio Thank you! That worked

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qm3ster picture qm3ster  路  4Comments

periabyte picture periabyte  路  7Comments

dan-kez picture dan-kez  路  5Comments

ozkxr picture ozkxr  路  3Comments

Scit picture Scit  路  4Comments