petko_stankoski
petko_stankoski

Reputation: 10713

Add image source in Jquery

In my MVC app, the images will b in the App_Data folder. I want to give the source to my img tag in Jquery. Here is how I do it:

var src1 = <%=Url.Content(Server.MapPath("/AppData/1.jpg"))%>
$("#imgLocation").attr("src", src1);

But it doesn't work. Why?

Upvotes: 6

Views: 50265

Answers (2)

Nitin S
Nitin S

Reputation: 7591

try following:

var src1 = '<%=Url.Content(Server.MapPath("~/AppData/1.jpg"))%>';
$("#imgLocation").attr("src", src1);

Upvotes: 17

mas-designs
mas-designs

Reputation: 7536

Because you are trying do access an HTML element which is called imgLocation You either have to use

  1. $('#imgLocation') if your img has an ID of id="imgLocation"
  2. or $('.imgLocation') if your img has a class of class="imgLocation"

Upvotes: 3

Related Questions