cleave.js icon indicating copy to clipboard operation
cleave.js copied to clipboard

Why there is no jquery version?

Open Bighamster opened this issue 8 years ago • 10 comments

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);

Bighamster avatar Apr 19 '18 08:04 Bighamster

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

sergx avatar Oct 04 '18 17:10 sergx

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

Bighamster avatar Oct 05 '18 08:10 Bighamster

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

nosir avatar Dec 16 '18 11:12 nosir

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 avatar Apr 04 '19 06:04 Swizzy

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

Bighamster avatar Jun 15 '19 08:06 Bighamster

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.

willl avatar Jan 28 '20 06:01 willl

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 avatar Feb 03 '20 16:02 armenio

@armenio Thank you! That worked

willl avatar Feb 03 '20 23:02 willl

Just a minor change for others.

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

kevyworks avatar Mar 20 '21 18:03 kevyworks

Can anyone please tell me how to import the addon for phone numbers?

DrYurets avatar Nov 30 '22 13:11 DrYurets