Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML
Hi All,
 

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

please replay with an example.
Posted 10-Jun-11 23:57pm
ks ravi844
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you have something like:
<img id="myImage" src="Images\myImage.jpg" />
Then,
var imgSource = document.getElementById('myImage').src ;
  Permalink  
Comments
ks ravi at 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
SAKryukov at 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
SAKryukov at 11-Jun-11 23:59pm
   
My 5.
--SA
S Mewara at 12-Jun-11 2:21am
   
Thanks SA.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Perhaps the element is not found. Try this:
 
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[^]
  Permalink  
Comments
ks ravi at 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
SAKryukov at 12-Jun-11 0:01am
   
Reasonable advice. Besides, Javascript can be debugged in Visual Studio with IE. My 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

<img id="img1" src="img1.jpg" />
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.
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[^]
  Permalink  
Comments
ks ravi at 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 at 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 at 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 at 11-Jun-11 9:28am
   
Open the View source of that page & see what's the image id.
SAKryukov at 12-Jun-11 0:00am
   
My 5. OP just fail to use right solution.
--SA

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100