-1

This is the code:

$(document).ready(function(){ 
function(dataObj){
  $.each(dataObj,function(n){
    if(dataObj.stream=="null"){
      var channelName=dataObj.display_name;
    }else{
      var channelName=dataObj.stream.display_name;
    }
  });//each bracket
}
})

The error is at function(dataObj). What am I missing?

Hasanat Jahan
  • 11
  • 2
  • 3

4 Answers4

0

Your function doesn't have a name. That is why the error coming.

Vineesh
  • 3,762
  • 20
  • 37
0

You can't have an anonymous function. You need to provide a function name or assign a function to a variable.

METHOD 1: Assign a function to a variable

$(document).ready(function(){ 
    var abc = function(dataObj){
       $.each(dataObj, function(n){
          if(dataObj.stream=="null"){
            var channelName=dataObj.display_name;
          }else{
            var channelName=dataObj.stream.display_name;
          }
       });//each bracket
    }

    abc();
});  

Here you go with an example https://jsfiddle.net/andnLfbx/

$(document).ready(function(){ 
 var abc = function(dataObj){
    console.log("abc");
   $.each(dataObj, function(n){
     if(dataObj.stream=="null"){
       var channelName=dataObj.display_name;
     }else{
       var channelName=dataObj.stream.display_name;
     }
   });//each bracket
 }
  
  abc();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

METHOD 2: Provide a function name

$(document).ready(function(){ 
    function abc(dataObj){
        $.each(dataObj, function(n){
           if(dataObj.stream=="null"){
                var channelName=dataObj.display_name;
            }else{
                var channelName=dataObj.stream.display_name;
            }
        });//each bracket
    }
});

Here you go with an example

$(document).ready(function(){ 
 function abc(dataObj){
    console.log("abc");
   $.each(dataObj, function(n){
     if(dataObj.stream=="null"){
       var channelName=dataObj.display_name;
     }else{
       var channelName=dataObj.stream.display_name;
     }
   });//each bracket
 }
  
  abc();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Source :

Basic Knowledge :

https://www.w3schools.com/js/js_function_definition.asp https://www.w3schools.com/js/js_functions.asp

Depth Knowledge

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
0

Into your code function name missing it should something like.

function <function_name>(dataObj) //like     function getData(dataObj)

This point is not mandatory but we need to try manged jQuery rules.

And at end of the function add semicolon }); in $(document).ready(function(){ function. **

So it will look like.

$(document).ready(function(){ 
   ..............
   ..............
});
  • A semicolon is not required there. There are even style guides like [standardjs](https://standardjs.com/) that suggest/enforce to not use those semicolons at all. – t.niese Jul 19 '17 at 04:59
  • You are right! But we are following jQuery standard https://api.jquery.com/ready/. – Emipro Technologies Pvt. Ltd. Jul 19 '17 at 05:06
  • jQuery uses those semicolons in the docs and if you contribute to jQuery you have to use them. But in your own project even if you use jQuery in it you are free to follow the style guides you want, otherwise you would have a problem if you use two libraries with different style guidelines, the important part is just to be constistent in the own project. – t.niese Jul 19 '17 at 05:27
0

It can be your server responding issue.
You can check what it responds in the: browser developer tools > network

izzaki
  • 258
  • 2
  • 4