Manually triggering an event in JS Helper CakePHP

I am stuck in a situation. I am using JS Helper. I used the following code.

<?php $this->Js->get('#client_id') ->event('change', $this->Js->request(array('action' => '../ajax/get_client_location_and_process'), array('update' => '#client_location_process', 'async' => false, 'dataExpression' => true, 'method' => 'post', 'evalScripts' => true, 'data' => $this->Js->serializeForm(array('isForm' => True, 'inline' => True)) ) ) ); 

I want to fire a change event when the page loads. If I use the document.ready method, then it does not work. and I could not find the JS Helper method, where we can explicitly trigger any event on the controls. Please offer code how I can execute jQuery trigger () as the functionality of form elements whenever I need it.

+6
source share
1 answer

Since you already learned about .trigger() in jQuery, you can simply use it along with your view code:

 <?php // Your view code ?> <script>$('#client_id').trigger('change');</script> 

Alternatively, if you still prefer to do this with PHP, you can create your own helper, for example:

 <?php class ArunjsHelper extends AppHelper { public $helpers = array('Html'); function trigger($element, $event, $options = array()) { return $this->Html->scriptBlock("$('$element').trigger('$event');"); } } 

Add ArunjsHelper to $helpers on the controller:

 <?php class SomeController extends Controller { public $helpers = array('Arunjs'); // Your controller code } 

Then you can call it from the view:

 <h1>Hello</h1> <p>Your usual view HTML code</p> <?php // Trigger the change event ?> <?php echo $this->Arunjs->trigger('#client_id', 'change'); ?> 
+12
source

Source: https://habr.com/ru/post/919553/


All Articles