Click here to Skip to main content
15,867,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I've img(id="img1") tag.I wanted to get that image name and have used following jscript but not working.

C#
var img1= document.getElementById('img1').src



please replay with an example.
Posted

Perhaps the element is not found. Try this:

JavaScript
var imgObject1 = document.getElementById('img1');
alert(imgObject1);
var imgSource1 = imgObject1.src;


If imgObject1 is null then the element was not found.

For examples see:http://www.w3schools.com/jsref/prop_img_src.asp[^]
 
Share this answer
 
Comments
ks ravi 11-Jun-11 5:40am    
thanks for reply.yes,its showing null and then
this error message:
Microsoft JScript runtime error: Object required
please suggest some other answers
Sergey Alexandrovich Kryukov 12-Jun-11 0:01am    
Reasonable advice. Besides, Javascript can be debugged in Visual Studio with IE. My 5.
--SA
XML
<img id="img1" src="img1.jpg" />

JavaScript
var imgSource = document.getElementById('img1').src;

Fortunately other answers are right. But I think you are using ASP.NET & master page & server Control(I mean runat="server")....If yes then use the below one.
JavaScript
var imgSource = document.getElementById('<% = img1.ClientID%>').src;

Otherwise you need to show your full code in your question.

Further Reading
How-to use ClientIDs in JavaScript[^]
 
Share this answer
 
Comments
ks ravi 11-Jun-11 8:01am    
Yes you are right,i'm using asp.net.I implemented this but once again showing this erroe:
Microsoft JScript runtime error: Object required

and at run time its showing like as before:
var imgSource = document.getElementById('img1').src;
please reply.
thatraja 11-Jun-11 8:06am    
Check my answer. OK here try this.
var imgSource = document.getElementById('<% = img1.ClientID%>').src;

BTW always include all details in your question so that you can get solutions quickly.
ks ravi 11-Jun-11 8:38am    
thanks for the replay.I'm getting same error:
Microsoft JScript runtime error: Object required.
And i need to tell you this that '.src' attribute is not showing in the list after 'getElementById(<% = img1.ClientID%>)'.I think somewhere i'm doing wrong.
please tell me where's it.
thatraja 11-Jun-11 9:28am    
Open the View source of that page & see what's the image id.
Sergey Alexandrovich Kryukov 12-Jun-11 0:00am    
My 5. OP just fail to use right solution.
--SA
If you have something like:
<img id="myImage" src="Images\myImage.jpg" />

Then,
JavaScript
var imgSource = document.getElementById('myImage').src ;
 
Share this answer
 
Comments
ks ravi 11-Jun-11 5:31am    
thanks for reply but this not working fine and showing error like;
Microsoft JScript runtime error: Object required
please suggest some other answers
Sergey Alexandrovich Kryukov 11-Jun-11 23:59pm    
This is correct; if it does not work, this is your bug. Show your code if you want someone to find the problem.
--SA
Sergey Alexandrovich Kryukov 11-Jun-11 23:59pm    
My 5.
--SA
Sandeep Mewara 12-Jun-11 2:21am    
Thanks SA.
<html>
<head>
<script>
function myfunction()
{
var x=document.getElementById("img1").name;
document.getElementById("demo").innerHTML = x;
var y=document.getElementById("img1").src;
document.getElementById("demo1").innerHTML = y;
}

</script>
</head>
<body>

XML
<p id="demo"></p>
<p id="demo1"></p>

<img src="E:/Sample Pictures/1.jpg" name="photo" width="500px" height="400" onclick="myfunction()" id="img1">
</body>






</html>
 
Share this answer
 
v3
Comments
King Fisher 19-Mar-15 14:24pm    
4 yr old Question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900