Drupal Answers Asked by zephirus on December 23, 2020
I was following this post on drupal.org – https://www.drupal.org/node/3075055
But I’m not getting the same result. I’m not experienced at all in javascript so maybe I’m missing something. Do I need to indicate where is my js file? The name of the js file is important?
My module code below:
function draws_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'bets_node_form':
$form['field_possible_returns']['und'][0]['value']['#process'] = array('my_custom_process');
break;
}
}
function my_custom_process($element, &$form_state, $complete_form){
$element['field_possible_returns'] = array(
'#type' => 'textfield',
'#size' => 8,
'#attributes' => array(
'readonly' => 'readonly',
'tabindex' => -1,
),
);
return $element;
}
The name of my module is draws
.
My javascript file (in the same folder of the module file):
(function ($) {
Drupal.behaviors.draws = {
attach: function (context, settings) {
alert('Its working!');
$('.field-name-field-bet-value').on('keyup change', function (){
var bet_value = $('.field-name-field-bet-value input').val();
var odd = $('.field-name-field-odd input').val();
var result = (odd * bet_value).toFixed(2);
$('.field-name-field-possible-returns input').val(result);
});
}
};
}(jQuery));
Here are the field names of my content type bets
:
Thank you.
Do I need to indicate where is my js file?
Of course, js files do not auto magically get loaded.
You need to tell Drupal about them, There are various ways, see Adding JavaScript to your theme or module.
function draws_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'bets_node_form':
$form['field_possible_returns']['und'][0]['value']['#process'] = array('my_custom_process');
// Load JS File
$form['#attached']['js'] = [
drupal_get_path('module', 'draws') . '/draws.js',
];
break;
}
}
Correct answer by No Sssweat on December 23, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP