Setting an HTML tag attribute using EJS or jQuery

On my explicit server, I display the data page as follows:

app.get('/people/:personID', function (req, res) {
  res.render("people/profile", {person: req.person });
});

In my profile.ejs file, I can access the data in the ejs tag, for example: <p><%= person.name %></p>

I cannot figure out how to change the attribute of the html tag to the value stored in this object.

This does not work: <img id="my_img" src=<%= person.picture %> alt="">
or this:$("#my_img").attr("src", <%= person.picture %>);

Also, if there is a better way to transfer this document to the html page and access it, I’m all ears (or eyes in this case). Thanks you

+4
source share
1 answer

You must include string values ​​in quotation marks.

In html:

<img id="my_img" src="<%= person.picture %>" alt="">

In jQuery:

$("#my_img").attr("src", "<%= person.picture %>");
+8
source

Source: https://habr.com/ru/post/1542844/


All Articles