jQuery-Mask-Plugin icon indicating copy to clipboard operation
jQuery-Mask-Plugin copied to clipboard

set mask in html attribute data-mask="" is not working

Open sergeymakoveev opened this issue 12 years ago • 19 comments

sergeymakoveev avatar Oct 28 '14 06:10 sergeymakoveev

https://github.com/igorescobar/jQuery-Mask-Plugin#problems--bugs

igorescobar avatar Oct 28 '14 12:10 igorescobar

@igorescobar same problem!

http://jsfiddle.net/agj42qxw/1/

jquery-mask

ghost avatar Oct 31 '14 17:10 ghost

Weird... my fiddle its working... http://jsfiddle.net/igorescobar/x2mhx0Ld/

igorescobar avatar Oct 31 '14 23:10 igorescobar

You cant call your javascript files before DOM Ready my friend. There is no way to jQuery Mask find your field in that way.

igorescobar avatar Oct 31 '14 23:10 igorescobar

@igorescobar Your fiddle works with JS call: $('.date').mask("00/00/0000"). Doesn't make sense to me to use the data attributes plus JS call. If I'm using data-mask="foo" means that I don't want to use $('.date-mask').mask('some-mask'). Does make sense to you?

I'm not telling that's a bug, but an improvement maybe.

ghost avatar Nov 05 '14 10:11 ghost

Who are telling you to use both?

[]'s Igor

On 05/11/2014, at 08:27, Richard Santos [email protected] wrote:

@igorescobar Your fiddle works with JS call: $('.date').mask("00/00/0000"). Doens't make sense to me to use the data attributes plus JS call. If I'm using data-mask="foo" means that I don't want to use $('.date-mask').mask('some-mask'). Does make sense to you?

I'm not telling that's a bug, but an improvement maybe.

— Reply to this email directly or view it on GitHub.

igorescobar avatar Nov 05 '14 11:11 igorescobar

@rsantostray my fiddle has both use cases (JS/data-mask).

igorescobar avatar Nov 05 '14 11:11 igorescobar

I think @igorescobar make as like to provide performance stuffs.

One watcher is created to each new calls to mask based on jQuery selector string. So don't make sense to jQueryMask create an void watcher just to provide that issue.

In most cases is better use javascript way instead data-* attribute.

You can use above line to do what you need.

// empty string only for trigger
$(function(){ $('.mask').mask(''); });

Take a look on issue #233 with some examples.

dgmike avatar Nov 05 '14 11:11 dgmike

@dgmike I think that what @rsantostray is trying to tell us is that data-mask attributes isn't working at all! I don't think the data-mask watcher is his issue right now. Am I right @rsantostray ?

igorescobar avatar Nov 05 '14 11:11 igorescobar

Hmmm... something hapens with my tests.

Assuming:

$.jMaskGlobals.watchDataMask= true

In this case:

<input data-mask="00 000 0000" class="mask" />
<script>$('.mask').mask('')</script>

:warning: the mask do not apply.

In this case:

<input data-mask="00 000 0000" class="mask" />

:warning: the mask do not apply.

in this case:

<input data-mask="00 000 0000" class="mask" />
<script>$('.ANY-ANOTHER').mask('')</script>

:white_check_mark: The mask is applyed, but I this that is wrong!

dgmike avatar Nov 05 '14 11:11 dgmike

@dgmike there is no such thing as using both at the same time. This is so weird that I don't even tested this cenario. You have to choose between HTML Notation or Javascript Notation.

@rsantostray I updated your fiddle in order to fetch our updates and to make it work: http://jsfiddle.net/agj42qxw/3/

igorescobar avatar Nov 05 '14 12:11 igorescobar

@rsantostray @dgmike I've made a few adjustments to make this kind of functionality easier to use. Hope you guys enjoy it.

igorescobar avatar Nov 06 '14 13:11 igorescobar

@igorescobar I'll test. Thanks man!

ghost avatar Nov 07 '14 16:11 ghost

hey @igorescobar ! first off, thanks for the great plugin. I'm also currently facing this issue. I am able to fully use the JS notation as intended. I believe what you confirmed in this thread is that the HTML notation should work without any JS; just importing the 'jquery.mask.min' file is enough. That doesn't work for me. I have my file loaded at the end as you've suggested. However, when using the HTML notation, nothing happens. I'm unable to set a mask on any kind of element. To clarify, does this feature work on any element? I've tried on input, div, label. Currently, the options and html (actually .cfm file for ColdFusion) look like this:

     $.jMaskGlobals = {
		maskElements: 'input,td,span,div,label',
		dataMaskAttr: '*[data-mask]',
		dataMask: true,
		translation: {
			'9': {pattern: /\d/},
			'0': {pattern: /\d/, optional: true}
		}
	};
<input data-mask="999-999-9999" type="text" value='#rc.clientInfo.getArrayOfOrganizationBranch()[1].getPhoneNumber()#'>

Hamlet-Tamaz avatar Dec 29 '17 18:12 Hamlet-Tamaz

Solution to work is calling this script after document is loaded:

$('[data-mask]').each(function() {
    $(this).mask($(this).attr("data-mask"));
});

ruicaramalho avatar Jan 08 '18 19:01 ruicaramalho

Tks ruicaramalho, works fine this way.

luancardosolc avatar Mar 02 '18 14:03 luancardosolc

@Hamlet-Tamaz I provided a working jsfiddle at the beginning of the thread (https://github.com/igorescobar/jQuery-Mask-Plugin/issues/230#issuecomment-61345468). As I said. You need to load jQuery Mask AFTER your HTML definitions. Even after doing that it still doesn't work?

igorescobar avatar Mar 02 '18 15:03 igorescobar

You can just run $(function(){ $.applyDataMask(); }); instead. It's cleaner.

JoshuaNovak919 avatar Apr 02 '18 23:04 JoshuaNovak919

Hi @igorescobar

I am using the same plugin with a bit older version, I am stuck with two partial views and masking. Tried to explain my issue on the link:

https://stackoverflow.com/questions/56965428/mask-not-working-in-jquery-with-two-partial-views-in-mvc Please get back to me on this, thanks

GauravKaushik01 avatar Jul 17 '19 09:07 GauravKaushik01