Solved

jQuery not working with Drupal 7 Webform fields.

Posted on 2013-05-13
4
619 Views
Last Modified: 2013-05-15
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
Comment
Question by:Modifier1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 13

Accepted Solution

by:
Arrow_1 earned 166 total points
ID: 39163202
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
 
LVL 17

Assisted Solution

by:nanharbison
nanharbison earned 167 total points
ID: 39163363
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
 
LVL 16

Assisted Solution

by:HagayMandel
HagayMandel earned 167 total points
ID: 39167136
Check whether the javascript is actually loaded. (look at 'page source').
0
 

Author Closing Comment

by:Modifier1000
ID: 39169524
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question