4

Jquery is not working with EJS, here is my EJS page. I can see EJS functionality working properly but jQuery functionality is not working. It does not show any error.

<!DOCTYPE html>
<html>
  <head>
    <title>Help Page</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />

  </head>
  <body>
    <h1>Help</h1>
    <p>This is Help Page</p>

    <h2> Member Data </h2>
    <% data.forEach(function(d){ %>

        <%= d.member_desc %>

    <% });%>

     <script type='text/javascript'>
        $(function(){
            alert('inside jquery');
        });
     </script>
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
  </body>
</html>

3 Answers 3

3

I know it is a bit late to answer this but i just want to add this comment, they have (jquery and ejs) different lifecycles one (ejs) compiles before rendering and jquery works after rendering, your problem has this kinds of cause

Sign up to request clarification or add additional context in comments.

Comments

2

You need to put your jQuery <script> before the other. Otherwise $ is not yet defined.

The Browser loads every script one after the other. Say we had scripts A and B.B use something from A, then A should be before B or it wouldn't work.

TL;DR

Change this:

<script type='text/javascript'>
        $(function(){
            alert('inside jquery');
        });
     </script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

To this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type='text/javascript'>
            $(function(){
                alert('inside jquery');
            });
</script>

Comments

1

Put jquery script in:

$(document).ready(function(){
    ...
});

Now script will be run when all documents loads.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.