CodingWonders90 CodingWonders90 - 4 years ago 64
Javascript Question

Javascript/Jquery- "$ is not defined- $function()" Error

I am trying to run a javascript/jquery function and firebug gets the error:

$ is not defined $(function()".


The javascript is placed inside a file called
core.js
and referenced by
index.php
. What causes this error?

JavaScript:

<script type="text/javascript">
var formObject = {
run : function(obj) {
if (obj.val() === '') {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
} else {
var id = obj.attr('id');
var v = obj.val();
jQuery.getJSON('/mod/update.php', { id : id, value : v }, function(data) {
if (!data.error) {
obj.next('.update').html(data.list).removeAttr('disabled');
} else {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
}
});
}
}
};
$(function() {

$('.update').live('change', function() {
formObject.run($(this));
});

});
</script>


PHP/html

<html>
<select name="main" id="category" class="update">
<option value="">Select one</option>
<? if (!empty($list)) { ?>
<? foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<? echo $row['name']; ?>
</option>

<? } ?>
<? } ?>

</select>
</html>

Answer Source

You must not have made JQuery available to your script.

Add this to the top of your file:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

This issue is related to jquery-javascript file not added to php/jsp/asp file properly. This goes out and gets the jquery code from the source. You could download that and reference it locally on the server which would be faster.

Or either one can directly link it to jquery or GoogleCDN or MicrosoftCDN.

How do add jQuery to your webpage.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download