Quantcast
Viewing all articles
Browse latest Browse all 3491

How to select DOM elements within EJS file

I was wondering what the easiest way is to select DOM elements in an EJS file?  I have an EJS file with the following contents:

  1. <table id='mytable'>
  2. <tr>
  3.     <td><%= message %></td>
  4. </tr>
  5. <tr>
  6.     <td>  <%== $.View("simpleejs.ejs", {}) %>  </td>
  7. </tr>
  8. </table>
  9. <div id='divtest'></div>
  10. <%
  11. alert("Outer HTML foo length: " + $('#foo').length);
  12. alert("Inner EJS div length: " + $('#divtest').length);
  13. %>

This is just a simple example I'm setting up to try and figure this out, then I will apply the solution to my actual page.  I can see that my first alert statement prints "1", because it is an element in the html file that I'm calling this EJS from.  My second alert statement is 0, however, so I can see that I'm unable to access the divtest element.  I'm guessing this might have something to do with the timing of the rendering?

Is there any way I can access DOM elements contained in my EJS file from within the same EJS file?  Note that while isn't not necessary to do in my example, I do need to in my real code, this was just a sample to figure out how to get it to work.

Viewing all articles
Browse latest Browse all 3491

Trending Articles