Solved

JQuery problem

Posted on 2014-12-05
5
219 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Map JavaScript API 4 40
Ajax Panel Not Updating When Triggered By Timer 4 44
is this a cms? 8 35
wordpress display sub menu only when click 12 23
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
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)

895 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

19 Experts available now in Live!

Get 1:1 Help Now