Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the attribute of an HTML tag with EJS or jQuery

On my express server I'm rendering a page with data 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 an ejs tag like so: <p><%= person.name %></p>

I can't figure how to change an attribute of an html tag to the value stored in this object though.

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

Also, if there's a better way to pass this document to the html page and access it I'm all ears(or eyes in this case). Thank you

like image 433
Elementary Avatar asked Jun 01 '14 17:06

Elementary


People also ask

Can I use EJS in HTML?

EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.

Should I use HTML or EJS?

When to use HTML/ EJS? It varies according to your application. If you want to render a static page then go for an HTML file and if you want to render a dynamic page where your data coming from various sources then you must choose an EJS file. Good for the static web page.


1 Answers

You have to include string values within quotes.

In html:

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

In jQuery:

$("#my_img").attr("src", "<%= person.picture %>");
like image 105
zishe Avatar answered Sep 29 '22 00:09

zishe