Solved

JQuery problem

Posted on 2014-12-05
5
220 Views
Last Modified: 2014-12-05
I changed a small script last night that is supposed to perform an asynchronous call and fill in the div via AJAX.

I changed the following code:
function getColAvg(sUser, dtData){	
$('#DivColAverages').html('<div align="center"><img src="images/ajax-loader.gif" /></div>');
jQuery.ajax({
	type: "GET",
	url: "options.php?option=ColAvg&dataDate=" + dtData + "&User=" +sUser,
	dataType: "html",
	success: function(response){
		$('#DivColAverages').html('');
		$('#DivColAverages').html(response);
	},
	error: function(){
		//alert("Error occured during Ajax request...");
	}
});		

Open in new window



to permit the addition of a unique suffix to the div ID so that I can have more than 1 of these on the page.
function getColAvg(sUser, dtData, unq=''){	
var divA = document.getElementById('DivColAverages' + unq);
divA.html('<div align="center"><img src="images/ajax-loader.gif" /></div>');
jQuery.ajax({
	type: "GET",
	url: "options.php?option=ColAvg&dataDate=" + dtData + "&User=" +sUser,
	dataType: "html",
	success: function(response){
		var divA = document.getElementById('DivColAverages' + unq);
		divA.html('');
		divA.html(response);
	},
	error: function(){
		//alert("Error occured during Ajax request...");
	}
});		

Open in new window


And ... it no longer does the call, as far as I can tell. Firebug permits me to see that line 3 executes, but after execution the div code remains
 
<div id="DivColAverages" class="f_r_f_p_body">
<script>
getColAvg('SCAM','11/28/2014');
</script>
</div>

Open in new window


Where am I messing this up?  Thanks!
0
Comment
Question by:Daniel Wilson
5 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 40483334
Did you check the console? Are you sure that you aren't getting a syntax error on the line
  function getColAvg(sUser, dtData, unq=''){

that isn't standard javascript. You'd have to do something like this:
  function getColAvg(sUser, dtData, unq){
    unq = unq || '';
0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 40483354
You're right, there is a syntax error.  That's what I get for doing Javascript past 3AM!
TypeError: divA.html is not a function
	

divA.html('<div align="center"><img src="images/ajax-loader.gif" /></div>');

Open in new window


Suggestions on what I should be doing?  I'll add the line you say as well to handle the optional parameter.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40483367
try this :
<!DOCTYPE html>
<html>
<head>
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
	<script>
		jQuery(function($) {
			function getColAvg(sUser, dtData, unq='') {	
				$('#DivColAverages' + unq).html('<div align="center"><img src="images/ajax-loader.gif" /></div>');
				jQuery.ajax({
					type: "GET",
					url: "options.php?option=ColAvg&dataDate=" + dtData + "&User=" +sUser,
					dataType: "html",
					success: function(response){
						$('#DivColAverages' + unq).html(response);
					},
					error: function(){
						alert("Error occured during Ajax request...");
					}
				});		
			}
			getColAvg('SCAM','11/28/2014');
		});
		
	</script>
</head>
<body>
	<div id="DivColAverages" class="f_r_f_p_body"></div>
</body>
</html>

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40483435
function getColAvg(sUser, dtData, unq){

becomes

function getColAvg(sUser, dtData, unq){
if(typeof(unq)==='undefined') unq = "";
0
 
LVL 32

Author Closing Comment

by:Daniel Wilson
ID: 40483549
Leakim971's solved it.  Thanks to all who participated!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now