Since you already learned about .trigger()
in jQuery, you can simply use it along with your view code:
<?php <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');
Then you can call it from the view:
<h1>Hello</h1> <p>Your usual view HTML code</p> <?php <?php echo $this->Arunjs->trigger('#client_id', 'change'); ?>
source share