views:

88

answers:

4

hi, im having an img tag in my html when click on image javascript function alerts the image src path..

<image src="angel_wall.jpg" onclick="do_some();return false;" />
+4  A: 

If you want to alert the image’s src path when clicking on it, try this:

<img src="angel_wall.jpg" onclick="do_some(this)">

And the do_some function:

function do_some(elem) {
    alert(elem.src);
}
Gumbo
<input type="hidden" id="imgsrc"/>this tag in another form, i want to set the value of elem.src to this hidden type, hw?
udhaya
@udhaya: instead of `alert(elem.src);`, you can do `document.getElementById('imgsrc').value = elem.src;`
Daniel Vassallo
@udhaya: So you want to change the value of the hidden input when clicking on the image? Try `document.getElementById("imgsrc").value=elem.src` instead of the `alert` call.
Gumbo
document.getElement can get but not set valuenot working :(
udhaya
+1  A: 

It may look like you mean this:

<img src="/path/angel_wall.jpg" onclick="alert(this.src);" />
Daniel Vassallo
A: 
function do_some(img) {
    alert(img.src);
}

<img src="angel_wall.jpg" onclick="do_some(this);" />

Note that 'src' will return the full URL in most browsers. To get only the filename, you'll have to do something like this:

var src = img.src;
if(src.indexOf('/') > 0)
    src = src.substring(src.lastIndexOf('/')+1);
alert(src);
David Hedlund
A: 
<img id="test" src="angel_wall.gif" onClick="alert(document.getElementById('test').src)"  />
LnDCobra
<input type="hidden" id="imgsrc"/> this tag in another form, i want to set the value of elem.src to this hidden type, how?
udhaya
If you have a new question, then please ask a new question, and don't tack it on as a comment on an answer.
David Dorward
sorry david, frm now i wil keep this
udhaya