Newbie Problem : Add jquery id on Form




$js =<<<JSCONTENT


window.maskMoney_0fa5526f = {"prefix":" Rp ","suffix":" ,-","precision":0,"affixesStay":true,"thousands":",","decimal":".","allowZero":true,"allowNegative":true};


JSCONTENT;

$this->registerJs($js, 	View::POS_HEAD);

$js2 = <<<JSCONTENT


jQuery("#w13-disp").maskMoney(maskMoney_0fa5526f);


var val = parseFloat(jQuery("#w13").val());

jQuery("#w13-disp").maskMoney('mask', val);

jQuery("#w13-disp").on('change keyup', function (e) {

     if (e.type ==='change' || (e.type === 'keyup' && (e.keyCode == 13 || e.which == 13))) {

         var out = jQuery("#w13-disp").maskMoney('unmasked')[0];

        jQuery("#w13").val(out).trigger('change');

     }

});

JSCONTENT;

$this->registerJs($js2);

 

The Scripts Above Works for the price Format,

i use it in this Script for example


<input type="text" id="w13-disp" class="form-control" name="w13-disp" value="1000">

But if i Use Active Form how to insert the JQuery ?

The Form Script


 <?= $form->field($model, 'price')->textInput(['type' => 'number','placeholder' =>  'Harga Jual'])?>

Hi Felatio,

I think you need to map the same ID of the input field in JS as well Html Element.

please find below example changed from your snippet.





<?php


$yourDesiredID = 'test-w13-disp';

$js = <<<JSCONTENT


window.maskMoney_0fa5526f = {"prefix":" Rp ","suffix":" ,-","precision":0,"affixesStay":true,"thousands":",","decimal":".","allowZero":true,"allowNegative":true};


JSCONTENT;

$this->registerJs($js, View::POS_HEAD);

$js2 = <<<JSCONTENT


jQuery("$yourDesiredID").maskMoney(maskMoney_0fa5526f);


var val = parseFloat(jQuery("#w13").val());

jQuery("$yourDesiredID").maskMoney('mask', val);

jQuery("$yourDesiredID").on('change keyup', function (e) {

     if (e.type ==='change' || (e.type === 'keyup' && (e.keyCode == 13 || e.which == 13))) {

         var out = jQuery("$yourDesiredID").maskMoney('unmasked')[0];

        jQuery("#w13").val(out).trigger('change');

     }

});

JSCONTENT;

$this->registerJs($js2);

?>


<?= $form->field($model, 'price')->textInput(['type' => 'number', 'placeholder' => 'Harga Jual', 'id' => $yourDesiredID]) ?>