AnswerBun.com

Javascript in Drupal

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:

enter image description here

Thank you.

One Answer

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

Add your own answers!

Related Questions

Render fields url in tpl.php

2  Asked on October 26, 2021 by yoric

   

How to use Views Page + Attachment to show parent and children nodes?

1  Asked on October 26, 2021 by brian-huether

 

Hide new topic link based on the forum topics

1  Asked on October 26, 2021 by dominick

 

set min – max values drupal date_popup timepicker

2  Asked on October 26, 2021 by lorenzo-de-bie

   

Action links available options / caching

1  Asked on October 26, 2021 by marius-ilie

 

How to sort the query results?

1  Asked on October 26, 2021 by badm

   

Alter a path alias depending on a node property

1  Asked on October 26, 2021 by fizzz69

 

Webform variants as blocks

2  Asked on October 26, 2021 by lordaro

   

How do I redirect a domain but keep the URL structure?

1  Asked on October 26, 2021 by georgina-smith

     

How do I translate the weekday in a date?

1  Asked on October 26, 2021 by david-brugmans

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP