How do i read data in ejs template which is selected from dropdown

5,451 views
Skip to first unread message

Maneesh Rao

unread,
Jul 26, 2016, 9:38:16 AM7/26/16
to nodejs

I have node/express Application which is using ejs template as views. I have a drop down in ejs which is generated dynamically and it is working fine. I want to show/display the value selected from dropdown in a text box but my code is not working . I am able to do it in couple of ways in plain HTML/JS but not working in ejs.


Here is my code:

It seems run() is getting executed on selecting option from dropdown menu .

Any help will be greatly appreciated 


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"  http-equiv="refresh" content="7" >
    <title>Flow Meter Dashboard </title>
</head>

<body>

<h1> <%= title%></h1> <br>

<ul>

<% for(var key in Data){ %>
      <%    var k=key;%>
        <%var v=Data[key];%>
        <li><%= k %> <%=":"%> <%=v%></li>
        <%} %>

</ul>

<form action="http://127.0.0.1:8081/" method="POST">

Water Recharge Amount: <br>
<!-- <input type="text"   name="WaterBalance"  value= wb    > -->
<input type="submit" name="Recharge_Amount" value="100" > 
<input type="submit" name="Recharge_Amount" value="200"><br>
<br><input type="submit" name="Recharge_Amount" value="500" > 
<input type="submit" name="Recharge_Amount" value="1000"><br>

<br> Switch ON RO Purifier: <input type="submit" name="Switch_ON" value="ON" > <br>
<br>Switch OFF RO Purifier: <input type="submit" name="Switch_OFF" value="OFF" > <br>

</form>
Select RO<br>
<input type="text" id="srt" placeholder="select RO id"><br>

<%function run() {%>
        <%var e = document.getElementById("drpdwn");%>
        <%var strUser = e.options[e.selectedIndex].text;%>
        <%document.getElementById("srt").value =strUser;%>
   <% }%>
<br>

<select id="drpdwn"  onchange="run();">
<option value="Select_RO" selected>Select RO</option>
<%for(var i=0;i<ro.length;i++){ %>
<option value='<%= ro[i]%>'/><%=ro[i]%> </option>
<% }%>                 
</select>
</body>
</html>

Ethan Garofolo

unread,
Jul 26, 2016, 11:45:15 PM7/26/16
to nodejs
Not entirely sure about the issue, but I do see what looks like some malformed HTML.  Near the bottom you have:

<option value='<%= ro[i]%>'/><%=ro[i]%> </option>

Notice the closing slash right after `'<%= ro[i]%>'`.  You then close it again at the end of the line.  Maybe clear that up first and see if you still have the problem?

yuefeic...@gmail.com

unread,
Nov 29, 2017, 9:08:41 PM11/29/17
to nodejs
Did you find solutions to this? To me, I can't even call document in js function since ejs cannot manipulate with dom objects directly. 
Reply all
Reply to author
Forward
0 new messages