JQuery function not defined

I am having trouble downloading this code. I am sure there is something to do with noConflict, but I can not get it right.

$(document).ready(
   function spouseName() {
      if ($('#maritalstatus').val() == 'married') {
         $('#spousename').attr("disabled", false);
      } else {
         $('#spousename').val('');
         $('#spousename').attr("disabled", true);
      }
   }
);

by the way, it works on IE, but not FF

+7
source share
5 answers

thanks for the info and answers, it seems this topic helped

Function not defined

firefox does not recognize the function name when it is inside the jquery document.ready function. what I did was wrapped it inward, although it seems unconventional. I just deleted the document and worked perfectly. It seems chrome and FF do not recognize function names inside this?

function spouseName() {
  if ($('#maritalstatus').val() == 'married') {
     $('#spousename').attr("disabled", false);
  } 
  else {
     $('#spousename').val('');
     $('#spousename').attr("disabled", true);
  }

}

+6
source

The jQuery not defined error occurs if you have not enabled jQuery or you can import it using your JavaScript.

It should be like this:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">  </script>
<script type="text/javascript" src="myjsfile.js"> </script>

myjsfile.js :

$(document).ready(function(){
    every function inside this
});
+4

() ()

:

$(document).ready(function() {
   function spouseName() { // your code }
   function anotherFunction() { }
});
+2

. $.noConflict? , :

<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    $.noConflict();
    jQuery(document).ready(function($) {
        if ($('#maritalstatus').val() == 'married') {
            $('#spousename').attr("disabled", false);
        }
        else {
            $('#spousename').val('');
            $('#spousename').attr("disabled", true);
        }
    });

    // Code that uses other library $ can follow here.
</script>

But in all cases, make sure you reference the jQuery script correctly before trying to use it.

+1
source

In some cases, jQuery cannot find the function due to scope problems . You can define a function as shown below, and an important task is to set the global scope of the function:

jQuery(document).ready(function($) { 
      function sample() { console.log('blah blah'); } 
      window.sample = sample; //export function sample to the globals.
})

or shorter form as shown below:

(function($) { 
      function sample() { console.log('blah blah'); } 
      window.sample = sample; //export function sample to the globals.
})(jQuery)

Hope this helps.

0
source

All Articles