Add Image Source in jQuery

In my MVC application, the images will be in the App_Data folder. I want to provide a source for 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 that will not work. Why?

+4
source share
2 answers

try the following:

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

Since you are trying to access an HTML element called imgLocation you should either use

  • $('#imgLocation') if your img has id="imgLocation"
  • or $('.imgLocation') if your img has class class="imgLocation"
+2
source

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


All Articles