Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function is not defined - uncaught referenceerror

Tags:

javascript

I have this "Uncaught ReferenceError: function is not defined" error which do not understand.

If I have

$(document).ready(function() {   function codeAddress() {     var address = document.getElementById("formatedAddress").value;     geocoder.geocode({       'address': address     }, function(results, status) {       if (status == google.maps.GeocoderStatus.OK) {         map.setCenter(results[0].geometry.location);       }     });   } }); 

and

<input type="image" src="btn.png" alt="" onclick="codeAddress()" /> <input type="text" name="formatedAddress" id="formatedAddress" value="" /> 

When I press the button it will return the "Uncaught ReferenceError".

But if I put the codeAddress() outside the $(document).ready(function(){}) then it working fine.

My intention is put the codeAddress() within the document.ready function.

like image 798
Peter Avatar asked Feb 21 '11 15:02

Peter


People also ask

How do I fix uncaught ReferenceError is not defined?

The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

Why does it say my function is not defined?

The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.

Why is my function not defined in HTML?

You're Calling the Function Before It's Defined If the code that calls your JavaScript function precedes that function's definition in your HTML document, you will come across the function is not defined error. This is very, very important to check and rule out before you proceed to investigating other probable causes.

How do I fix reference error in JavaScript?

Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.


2 Answers

The problem is that codeAddress() doesn't have enough scope to be callable from the button. You must declare it outside the callback to ready():

function codeAddress() {     var address = document.getElementById("formatedAddress").value;     geocoder.geocode( { 'address': address}, function(results, status) {         if (status == google.maps.GeocoderStatus.OK) {             map.setCenter(results[0].geometry.location);         }     }); }   $(document).ready(function(){     // Do stuff here, including _calling_ codeAddress(), but not _defining_ it! }); 
like image 173
Humberto Avatar answered Oct 04 '22 17:10

Humberto


How about removing the onclick attribute and adding an ID:

<input type="image" src="btn.png" alt="" id="img-clck" /> 

And your script:

$(document).ready(function(){     function codeAddress() {         var address = document.getElementById("formatedAddress").value;         geocoder.geocode( { 'address': address}, function(results, status) {             if (status == google.maps.GeocoderStatus.OK) {                 map.setCenter(results[0].geometry.location);             }         });     }     $("#img-clck").click(codeAddress); }); 

This way if you need to change the function name or whatever no need to touch the html.

like image 40
ifaour Avatar answered Oct 04 '22 15:10

ifaour