skip to Main Content

My javascript component is not rendering and I’m getting the following error:

TypeError: $(...).cytoscape is not a function

Here is the code that throws the error.

Essentially I’m trying to isolate what javascript renders to the contents of the row. When I run this without nesting it in a container then It work fine.

<!DOCTYPE html>
<html>
  <head>

    <title>Main Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/static/css/graph4.css">


    <!--[if lt IE 9]>
          <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
          <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->


  </head>
  <body>
    
    
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
       <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
  <div class="container-fluid">


  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12" style="background-color:lavender;">
	  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
	  <script src="http://cytoscape.github.io/cytoscape.js/api/cytoscape.js-latest/cytoscape.min.js"></script>
          <script>
		$(function(){ // on dom ready
		
		$('#cy').cytoscape({
		    layout: {
    		    name: 'cose',
		    padding: 10,
		    randomize: true
		  },
		  
		  style: cytoscape.stylesheet()
		    .selector('node')
		      .css({
		        'shape': 'data(faveShape)',
		        'width': 'mapData(weight, 40, 80, 20, 60)',
		        'content': 'data(name)',
		        'text-valign': 'center',
		        'text-outline-width': 2,
		        'text-outline-color': 'data(faveColor)',
		        'background-color': 'data(faveColor)',
		        'color': '#fff'
		      })
		    .selector(':selected')
		      .css({
		        'border-width': 3,
		        'border-color': '#333'
		      })
		    .selector('edge')
		      .css({
		        'curve-style': 'bezier',
		        'opacity': 0.666,
		        'width': 'mapData(strength, 70, 100, 2, 6)',
		        'target-arrow-shape': 'triangle',
		        'source-arrow-shape': 'circle',
		        'line-color': 'data(faveColor)',
		        'source-arrow-color': 'data(faveColor)',
		        'target-arrow-color': 'data(faveColor)'
		      })
		    .selector('edge.questionable')
		      .css({
		        'line-style': 'dotted',
		        'target-arrow-shape': 'diamond'
		      })
		    .selector('.faded')
		      .css({
		        'opacity': 0.25,
		        'text-opacity': 0
     		 }),
  
		  elements: {
		    nodes: [
		      { data: { id: 'j', name: 'Jerry', weight: 65, faveColor: '#6FB1FC', faveShape: 'triangle' } },
		      { data: { id: 'e', name: 'Elaine', weight: 45, faveColor: '#EDA1ED', faveShape: 'ellipse' } },
		      { data: { id: 'k', name: 'Kramer', weight: 75, faveColor: '#86B342', faveShape: 'octagon' } },
		      { data: { id: 'g', name: 'George', weight: 70, faveColor: '#F5A45D', faveShape: 'rectangle' } }
		    ],
		    edges: [
		      { data: { source: 'j', target: 'e', faveColor: '#6FB1FC', strength: 90 } },
		      { data: { source: 'j', target: 'k', faveColor: '#6FB1FC', strength: 70 } },
		      { data: { source: 'j', target: 'g', faveColor: '#6FB1FC', strength: 80 } },
		     
		      { data: { source: 'e', target: 'j', faveColor: '#EDA1ED', strength: 95 } },
		      { data: { source: 'e', target: 'k', faveColor: '#EDA1ED', strength: 60 }, classes: 'questionable' },
		      
		      { data: { source: 'k', target: 'j', faveColor: '#86B342', strength: 100 } },
		      { data: { source: 'k', target: 'e', faveColor: '#86B342', strength: 100 } },
		      { data: { source: 'k', target: 'g', faveColor: '#86B342', strength: 100 } },
		      
		      { data: { source: 'g', target: 'j', faveColor: '#F5A45D', strength: 90 } }
		    ]
		  },
		  
		  ready: function(){
		    window.cy = this;
		    
		    // giddy up
		  }
		});
		
		}); // on dom ready
		
		
		
		
		
		
		
		
		</script>
          <div id="cy"></div>
	  <p>Test</p>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12" style="background-color:lavenderblush";>
        Timeline
    </div>
  </div>







   





</div>

<footer class="footer">
<p>
<center>
<span class="glyphicon glyphicon-copyright-mark" aria-hidden="true"></span>
Test
</center>
</p>
</footer>

    
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </body>
</html>

2

Answers


  1. you need to pass the jQuery object thusly :

    (function($) {
         //dom and jquery is ready
     })(jQuery);
    

    try it :

    <!DOCTYPE html>
    <html>
      <head>
    
        <title>Main Page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Bootstrap -->
        <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    
    
    
        <!--[if lt IE 9]>
              <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
              <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    
    
      </head>
      <body>
        
        
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Brand</a>
        </div>
    
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
            <li><a href="#">Link</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">Separated link</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>
          <form class="navbar-form navbar-left">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
          </form>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Link</a></li>
            <li class="dropdown">
           <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">Separated link</a></li>
              </ul>
            </li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
      <div class="container-fluid">
    
    
      <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12" style="background-color:lavender;">
    	  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    	  <script src="//cdnjs.cloudflare.com/ajax/libs/cytoscape/2.7.13/cytoscape.min.js"></script>
              <script>
    		(function($) { // on dom ready
    		
    		$('#cy').cytoscape({
    		    layout: {
        		    name: 'cose',
    		    padding: 10,
    		    randomize: true
    		  },
    		  
    		  style: cytoscape.stylesheet()
    		    .selector('node')
    		      .css({
    		        'shape': 'data(faveShape)',
    		        'width': 'mapData(weight, 40, 80, 20, 60)',
    		        'content': 'data(name)',
    		        'text-valign': 'center',
    		        'text-outline-width': 2,
    		        'text-outline-color': 'data(faveColor)',
    		        'background-color': 'data(faveColor)',
    		        'color': '#fff'
    		      })
    		    .selector(':selected')
    		      .css({
    		        'border-width': 3,
    		        'border-color': '#333'
    		      })
    		    .selector('edge')
    		      .css({
    		        'curve-style': 'bezier',
    		        'opacity': 0.666,
    		        'width': 'mapData(strength, 70, 100, 2, 6)',
    		        'target-arrow-shape': 'triangle',
    		        'source-arrow-shape': 'circle',
    		        'line-color': 'data(faveColor)',
    		        'source-arrow-color': 'data(faveColor)',
    		        'target-arrow-color': 'data(faveColor)'
    		      })
    		    .selector('edge.questionable')
    		      .css({
    		        'line-style': 'dotted',
    		        'target-arrow-shape': 'diamond'
    		      })
    		    .selector('.faded')
    		      .css({
    		        'opacity': 0.25,
    		        'text-opacity': 0
         		 }),
      
    		  elements: {
    		    nodes: [
    		      { data: { id: 'j', name: 'Jerry', weight: 65, faveColor: '#6FB1FC', faveShape: 'triangle' } },
    		      { data: { id: 'e', name: 'Elaine', weight: 45, faveColor: '#EDA1ED', faveShape: 'ellipse' } },
    		      { data: { id: 'k', name: 'Kramer', weight: 75, faveColor: '#86B342', faveShape: 'octagon' } },
    		      { data: { id: 'g', name: 'George', weight: 70, faveColor: '#F5A45D', faveShape: 'rectangle' } }
    		    ],
    		    edges: [
    		      { data: { source: 'j', target: 'e', faveColor: '#6FB1FC', strength: 90 } },
    		      { data: { source: 'j', target: 'k', faveColor: '#6FB1FC', strength: 70 } },
    		      { data: { source: 'j', target: 'g', faveColor: '#6FB1FC', strength: 80 } },
    		     
    		      { data: { source: 'e', target: 'j', faveColor: '#EDA1ED', strength: 95 } },
    		      { data: { source: 'e', target: 'k', faveColor: '#EDA1ED', strength: 60 }, classes: 'questionable' },
    		      
    		      { data: { source: 'k', target: 'j', faveColor: '#86B342', strength: 100 } },
    		      { data: { source: 'k', target: 'e', faveColor: '#86B342', strength: 100 } },
    		      { data: { source: 'k', target: 'g', faveColor: '#86B342', strength: 100 } },
    		      
    		      { data: { source: 'g', target: 'j', faveColor: '#F5A45D', strength: 90 } }
    		    ]
    		  },
    		  
    		  ready: function(){
    		    window.cy = this;
    		    
    		    // giddy up
    		  }
    		});
    		
    		}); // on dom ready
    		
    		
    		
    		
    		
    		
    		
    		
    		</script>
              <div id="cy"></div>
    	  <p>Test</p>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12" style="background-color:lavenderblush";>
            Timeline
        </div>
      </div>
    
    
    
    
    
    
    
       
    
    
    
    
    
    </div>
    
    <footer class="footer">
    <p>
    <center>
    <span class="glyphicon glyphicon-copyright-mark" aria-hidden="true"></span>
    Test
    </center>
    </p>
    </footer>
    
        
        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
      </body>
    </html>
    Login or Signup to reply.
  2. $('#cy').cytoscape({
      layout: {
        name: 'cose',
        padding: 10,
        randomize: true
      },
      ...
    };
    

    needs to be changed to

    var cy = cytoscape({
      container: $('#cy'),
      layout: {
          name: 'cose',
        padding: 10,
        randomize: true
      },
      ...
    };
    

    as per the directions at http://js.cytoscape.org/#getting-started/initialisation

    This will resolve your error, but I’m not sure if it’s rendering what you were hoping it would render. I hope this at least gets you one step closer to your desired behavior!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search