Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • Last Modified:

jQuery not working with Drupal 7 Webform fields.

Hello Experts,

I am trying to do a quick alert on the 'changing' of a textfield using jQuery in Drupal.  My theme .info files does have:
scripts[] = info.js

Open in new window

and the .js file is in the same dir as the .info file (for now)

The code in the .js file is:
$(document).ready(function() {
	$('#edit-submitted-num1').change(function() {
		alert('num1 changed!');
	});
});

Open in new window


It's not giving me an alert.  Attahed is the simpel webform I created.

Sorta stuck on this simple code.

#edit-submitted-num1
0
Modifier1000
Asked:
Modifier1000
3 Solutions
 
Aaron FeledyDrupal Developer and ConsultantCommented:
With Drupal you usually need to use "jQuery" instead of "$" to avoid conflicts. Should look more like this:
jQuery(document).ready(function($) {
  $('#edit-submitted-num1').change(function() {
    alert('num1 changed!');
  });
});

Open in new window


More here: http://drupal.org/node/171213
0
 
nanharbisonCommented:
Make sure you choose the text format as PHP code. If that is not a choice, go to the modules page and enable it.
0
 
HagayMandelCommented:
Check whether the javascript is actually loaded. (look at 'page source').
0
 
Modifier1000Author Commented:
Got it to work!  
Actually the help from all of you was very helpful, thanks!

(function ($) {
$(document).ready(function(){

	var content = '';
	

	 $('#edit-submitted-cat-1-reim-prior,#edit-submitted-cat-1-reim-curr,#edit-submitted-cat-1-reim-total').change(function() {
		 content1 = parseInt($('#edit-submitted-cat-1-reim-prior').val(), 10);
		 content2 = parseInt($('#edit-submitted-cat-1-reim-curr').val(), 10);
		 contentTotal = parseInt(content1 + content2);

		$('#edit-submitted-cat-1-reim-total').val(contentTotal);
	 });
   });
})(jQuery);

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now