Setting the attribute of an HTML tag with EJS or jQuery

Elementary picture Elementary · Jun 1, 2014 · Viewed 14k times · Source

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

Answer

zishe picture zishe · Jun 2, 2014

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 %>");