Solved

JQuery problem

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem in javascript 17 49
Change javascript filter code from OR logic to AND 5 36
hide and show spans from dropdown selection 3 31
Jquery syntax 12 23
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)

740 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