views:

58

answers:

3
<script type="text/javascript">
    function GetSrc(elem)
    {
        alert ( elem.src );
    }

</script>

<img src="images/yourimage.extn" id="img1" onclick="GetSrc(this);" />

i want to convert elem.src to string, String(elem.src); not working

A: 

You can already use the source as a string.

alert( document.getElementById("img1").src ); // images/yourimage.extn

You can set it too:

function GetSrc(elem) {
  elem.src = "http://kol.coldfront.net/thekolwiki/images/c/ca/String.gif";
}​
Jonathan Sampson
A: 

It is a string itself. No need to again convert to string.

alert ( typeof(elem.src) );

will give you a string

rahul
+2  A: 

It's already a string. You could also try:

var srcString = new String(elem.src);

but it is unnecessary.

Darin Dimitrov