Here is my EJS file
JavaScript
x
36
36
1
<!DOCTYPE html>
2
<html lang="en" dir="ltr">
3
<head>
4
<meta charset="utf-8">
5
<title>Sart Plug</title>
6
<script src="http://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
7
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
8
<link rel="stylesheet" href="/css/usrDashboard.css">
9
<link rel="icon" href="/images/plugimg.png" type="image/x-icon" />
10
</head>
11
<body>
12
<div class="container">
13
<div class="header">
14
<a href="#" class="logo">Select Device</a>
15
<div class="header-right">
16
<a href="/logout">Log Out</a>
17
</div>
18
</div>
19
<div class="jumbotron" id="jumbo">
20
<p><%=data.noResult%></p>
21
<div class="row">
22
<% for(var i=0; i < data.result.length; i++) { %>
23
<div class="col-25">
24
<a href="/plugDashboard">
25
<p><img class="img-fluid" src="/images/plugimg.png" alt="Plug Image"></p>
26
<p><h3><%= data.result[i].device_name %></h3></p>
27
<p><h6><%= data.result[i].device_address %></h6></p>
28
</a>
29
</div>
30
<% } %>
31
</div>
32
</div>
33
</div>
34
</body>
35
</html>
36
My routes are in the below route:-
JavaScript
1
23
23
1
app.get('/usrDashboard', checkAuthenticted, (req, res) => {
2
let email = req.user.email;
3
con.query("SELECT device_name, device_address FROM pluglist WHERE email ='"+ email +"' " , (err, result, fields)=> {
4
if(err) throw err;
5
console.log(result);
6
if (result.length === 0) {
7
res.render('usrDashboard',{data:{email: email,
8
result:result,
9
noResult:'You have no saved device'}});
10
}
11
else {
12
13
res.render('usrDashboard',{data:{email: email,
14
result:result}});
15
}
16
});
17
})
18
19
app.get('/plugDashboard', checkAuthenticted, (req, res) => {
20
console.log(req);
21
res.render('plugDashboard');
22
})
23
SO here I want, whenever I click on any of the div I want to print the mac id in log from the respective div in plugDashboard section. So is there any way to do so? I searched for many solutions none of them were helpful for me.
Advertisement
Answer
You can add new GET
express route, for example:
JavaScript
1
6
1
router.get('/get_mac_info/:mac_id', function(req, res, next) {
2
res.json({
3
mac_id: 'test_id'
4
5
});
6
});
and after that just add ajax
call on click event:
JavaScript
1
9
1
$(function () {
2
$(document).on('click', '.col-25', function (e) {
3
$.get('/get_mac_info/' + mac_id, function(data){
4
$('.print_container').text(data)
5
})
6
e.preventDefault();
7
});
8
9
});