Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why there is no jquery version? #341

Closed
Bighamster opened this issue Apr 19, 2018 · 10 comments
Closed

Why there is no jquery version? #341

Bighamster opened this issue Apr 19, 2018 · 10 comments

Comments

@Bighamster
Copy link

Bighamster commented Apr 19, 2018

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);
@sergx
Copy link

sergx commented Oct 4, 2018

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

@Bighamster
Copy link
Author

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

@nosir
Copy link
Owner

nosir commented Dec 16, 2018

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 nosir closed this as completed Dec 16, 2018
@Swizzy
Copy link

Swizzy commented Apr 4, 2019

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

@Bighamster
Copy link
Author

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

@willl
Copy link

willl commented Jan 28, 2020

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.

@armenio
Copy link

armenio commented Feb 3, 2020

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

@willl
Copy link

willl commented Feb 3, 2020

@armenio Thank you! That worked

@kevyworks
Copy link

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

@DrYurets
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants